From f654056f1868fd3b72f62212c6e2b89b9837375d Mon Sep 17 00:00:00 2001 From: Loay Date: Mon, 10 Apr 2017 17:55:29 -0400 Subject: [PATCH] Fix Update bug when id not found --- lib/sql.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/sql.js b/lib/sql.js index 0556cfa..bccead2 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -889,10 +889,26 @@ SQLConnector.prototype._constructUpdateQuery = function(model, where, fields) { * @param {Function} cb The callback function */ SQLConnector.prototype.update = function(model, where, data, options, cb) { + var self = this; var stmt = this.buildUpdate(model, where, data, options); - this._executeAlteringQuery(model, stmt.sql, stmt.params, options, cb || NOOP); + this._executeAlteringQuery(model, stmt.sql, stmt.params, options, function(err, info) { + if (err) return cb(err); + if (where.id && info.count === 0) { + return cb(errorIdNotFoundForUpdate(where.id)); + } else { + return cb(null, info) || NOOP; + } + }); }; +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 all instances that match the where clause with the given data * @param {String} model The model name