From d2d426c93b82aaefface3a48ebadbc260f712289 Mon Sep 17 00:00:00 2001 From: Loay Gewily Date: Fri, 14 Apr 2017 01:51:31 -0400 Subject: [PATCH] Fix update bug when id not found --- lib/sql.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/sql.js b/lib/sql.js index 0556cfa..cea7bea 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -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