From 4108db894558daf12325dfed4c565094399c5f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20=C3=96tjengerdes?= Date: Fri, 13 Jul 2018 16:18:25 +0200 Subject: [PATCH] fix: return null when findById/findOne returns 404 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maxim Sharai Co-authored-by: Jannis Ötjengerdes --- lib/remote-connector.js | 19 ++++++++++++++++++- test/remote-models.test.js | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/remote-connector.js b/lib/remote-connector.js index eef404e..0e28c04 100644 --- a/lib/remote-connector.js +++ b/lib/remote-connector.js @@ -16,6 +16,8 @@ var jutil = require('loopback-datasource-juggler/lib/jutil'); var RelationMixin = require('./relations'); var InclusionMixin = require('loopback-datasource-juggler/lib/include'); +var findMethodNames = ['findById', 'findOne']; + /** * Export the RemoteConnector class. */ @@ -120,6 +122,11 @@ function createProxyMethod(Model, remotes, remoteMethod) { } else { callback = utils.createPromiseCallback(); } + var callbackPromise = callback.promise; + + if (findMethodNames.includes(remoteMethod.name)) { + callback = proxy404toNull(callback); + } if (remoteMethod.isStatic) { remotes.invoke(remoteMethod.stringName, args, callback); @@ -128,7 +135,17 @@ function createProxyMethod(Model, remotes, remoteMethod) { remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback); } - return callback.promise; + return callbackPromise; + } + + function proxy404toNull(cb) { + return function(err, data) { + if (err && err.code === 'MODEL_NOT_FOUND') { + cb(null, null); + return; + } + cb(err, data); + }; } scope[remoteMethod.name] = remoteMethodProxy; diff --git a/test/remote-models.test.js b/test/remote-models.test.js index 1d1454f..edb1207 100644 --- a/test/remote-models.test.js +++ b/test/remote-models.test.js @@ -191,9 +191,8 @@ describe('Remote model tests', function() { ClientModel.deleteById(user.id, function(err) { if (err) return done(err); ClientModel.findById(user.id, function(err, notFound) { + if (err) return done(err); assert.equal(notFound, null); - assert(err && err.statusCode === 404, - 'should have failed with HTTP 404'); done(); }); }); @@ -202,6 +201,15 @@ describe('Remote model tests', function() { }); describe('Model.findById(id, callback)', function() { + it('should return null when an instance does not exist', + function(done) { + ClientModel.findById(23, function(err, notFound) { + if (err) return done(err); + assert.equal(notFound, null); + done(); + }); + }); + it('should find an instance by id from the attached data source', function(done) { ServerModel.create({first: 'michael', last: 'jordan', id: 23}, @@ -218,6 +226,32 @@ describe('Remote model tests', function() { }); }); + describe('Model.findOne([filter], callback)', function() { + it('should return null when an instance does not exist', + function(done) { + ClientModel.findOne({where: {id: 24}}, function(err, notFound) { + if (err) return done(err); + assert.equal(notFound, null); + done(); + }); + }); + + it('should find an instance from the attached data source', + function(done) { + ServerModel.create({first: 'keanu', last: 'reeves', id: 24}, + function(err) { + if (err) return done(err); + ClientModel.findOne({where: {id: 24}}, function(err, user) { + if (err) return done(err); + assert.equal(user.id, 24); + assert.equal(user.first, 'keanu'); + assert.equal(user.last, 'reeves'); + done(); + }); + }); + }); + }); + describe('Model.count([query], callback)', function() { it('should return the count of Model instances from both data source', function(done) {