Fix update bug when id not found
This commit is contained in:
parent
6bb07257d3
commit
d2d426c93b
17
lib/sql.js
17
lib/sql.js
|
@ -803,9 +803,24 @@ Connector.defineAliases(SQLConnector.prototype, 'destroyAll', ['deleteAll']);
|
||||||
SQLConnector.prototype.updateAttributes = function(model, id, data, options, cb) {
|
SQLConnector.prototype.updateAttributes = function(model, id, data, options, cb) {
|
||||||
if (!isIdValuePresent(id, cb)) return;
|
if (!isIdValuePresent(id, cb)) return;
|
||||||
var where = this._buildWhereObjById(model, id, data);
|
var where = this._buildWhereObjById(model, id, data);
|
||||||
this.updateAll(model, where, data, options, cb);
|
this.updateAll(model, where, data, options, function(err, info) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (info.count === 0) {
|
||||||
|
return cb(errorIdNotFoundForUpdate(where.id));
|
||||||
|
} else {
|
||||||
|
return cb(null, info);
|
||||||
|
};
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function errorIdNotFoundForUpdate(idValue) {
|
||||||
|
var msg = g.f(
|
||||||
|
'Could not update attributes. {{Object}} with {{id}} %s does not exist!', idValue);
|
||||||
|
var error = new Error(msg);
|
||||||
|
error.statusCode = error.status = 404;
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace attributes for a given model instance
|
* Replace attributes for a given model instance
|
||||||
* @param {String} model The model name
|
* @param {String} model The model name
|
||||||
|
|
Loading…
Reference in New Issue