Fix update bug when id not found

This commit is contained in:
Loay Gewily 2017-04-14 01:51:31 -04:00
parent 6bb07257d3
commit d2d426c93b
1 changed files with 16 additions and 1 deletions

View File

@ -803,9 +803,24 @@ Connector.defineAliases(SQLConnector.prototype, 'destroyAll', ['deleteAll']);
SQLConnector.prototype.updateAttributes = function(model, id, data, options, cb) {
if (!isIdValuePresent(id, cb)) return;
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
* @param {String} model The model name