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) {