fix: return null when findById/findOne returns 404
Co-authored-by: Maxim Sharai <maxim.sharai@tispr.com> Co-authored-by: Jannis Ötjengerdes <joetjengerdes@rightmart.de>
This commit is contained in:
parent
1a7b1be96d
commit
4108db8945
|
@ -16,6 +16,8 @@ var jutil = require('loopback-datasource-juggler/lib/jutil');
|
||||||
var RelationMixin = require('./relations');
|
var RelationMixin = require('./relations');
|
||||||
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
|
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
|
||||||
|
|
||||||
|
var findMethodNames = ['findById', 'findOne'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export the RemoteConnector class.
|
* Export the RemoteConnector class.
|
||||||
*/
|
*/
|
||||||
|
@ -120,6 +122,11 @@ function createProxyMethod(Model, remotes, remoteMethod) {
|
||||||
} else {
|
} else {
|
||||||
callback = utils.createPromiseCallback();
|
callback = utils.createPromiseCallback();
|
||||||
}
|
}
|
||||||
|
var callbackPromise = callback.promise;
|
||||||
|
|
||||||
|
if (findMethodNames.includes(remoteMethod.name)) {
|
||||||
|
callback = proxy404toNull(callback);
|
||||||
|
}
|
||||||
|
|
||||||
if (remoteMethod.isStatic) {
|
if (remoteMethod.isStatic) {
|
||||||
remotes.invoke(remoteMethod.stringName, args, callback);
|
remotes.invoke(remoteMethod.stringName, args, callback);
|
||||||
|
@ -128,7 +135,17 @@ function createProxyMethod(Model, remotes, remoteMethod) {
|
||||||
remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
|
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;
|
scope[remoteMethod.name] = remoteMethodProxy;
|
||||||
|
|
|
@ -191,9 +191,8 @@ describe('Remote model tests', function() {
|
||||||
ClientModel.deleteById(user.id, function(err) {
|
ClientModel.deleteById(user.id, function(err) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
ClientModel.findById(user.id, function(err, notFound) {
|
ClientModel.findById(user.id, function(err, notFound) {
|
||||||
|
if (err) return done(err);
|
||||||
assert.equal(notFound, null);
|
assert.equal(notFound, null);
|
||||||
assert(err && err.statusCode === 404,
|
|
||||||
'should have failed with HTTP 404');
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -202,6 +201,15 @@ describe('Remote model tests', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Model.findById(id, callback)', 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',
|
it('should find an instance by id from the attached data source',
|
||||||
function(done) {
|
function(done) {
|
||||||
ServerModel.create({first: 'michael', last: 'jordan', id: 23},
|
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() {
|
describe('Model.count([query], callback)', function() {
|
||||||
it('should return the count of Model instances from both data source',
|
it('should return the count of Model instances from both data source',
|
||||||
function(done) {
|
function(done) {
|
||||||
|
|
Loading…
Reference in New Issue