From 3ee3ba3dcfa4c880034c703bc93e3b6a9a4e204f Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 8 Jun 2015 13:01:16 -0700 Subject: [PATCH] Allow 0 as the FK See https://github.com/strongloop/loopback/issues/967 --- lib/relation-definition.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 5fe8af3d..fea24b65 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -783,7 +783,7 @@ HasMany.prototype.findById = function (fkId, options, cb) { return cb(err); } // Check if the foreign key matches the primary key - if (inst[fk] && inst[fk].toString() === modelInstance[pk].toString()) { + if (inst[fk] != null && inst[fk].toString() === modelInstance[pk].toString()) { cb(null, inst); } else { err = new Error('Key mismatch: ' + modelFrom.modelName + '.' + pk @@ -1483,7 +1483,7 @@ BelongsTo.prototype.related = function (condOrRefresh, options, cb) { return cb(null, null); } // Check if the foreign key matches the primary key - if (inst[pk] && modelInstance[fk] + if (inst[pk] != null && modelInstance[fk] != null && inst[pk].toString() === modelInstance[fk].toString()) { self.resetCache(inst); cb(null, inst); @@ -1891,7 +1891,7 @@ HasOne.prototype.related = function (condOrRefresh, options, cb) { return cb(null, null); } // Check if the foreign key matches the primary key - if (inst[fk] && modelInstance[pk] + if (inst[fk] != null && modelInstance[pk] != null && inst[fk].toString() === modelInstance[pk].toString()) { self.resetCache(inst); cb(null, inst); @@ -3005,7 +3005,10 @@ ReferencesMany.prototype.findById = function (fkId, options, cb) { } var currentIds = ids.map(function(id) { return id.toString(); }); - var id = (inst[pk] || '').toString(); // mongodb + var id = ''; + if (inst[pk] != null) { + id = inst[pk].toString(); + } // Check if the foreign key is amongst the ids if (currentIds.indexOf(id) > -1) {