From 0d7dcdd0437321a8716ab75c27fa45df7ba11ce1 Mon Sep 17 00:00:00 2001 From: ningsuhen Date: Tue, 16 Jun 2015 11:10:00 +0530 Subject: [PATCH] check object exists before setting __cachedRelations --- lib/include.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/include.js b/lib/include.js index 7e3dab96..30f14313 100644 --- a/lib/include.js +++ b/lib/include.js @@ -390,6 +390,7 @@ Inclusion.include = function (objects, include, options, cb) { var targetId = target[modelToIdName]; var objList = targetObjsMap[targetId.toString()]; async.each(objList, function (obj, next) { + if (!obj) return next(); obj.__cachedRelations[relationName].push(target); processTargetObj(obj, next); }, next); @@ -467,6 +468,7 @@ Inclusion.include = function (objects, include, options, cb) { function linkManyToMany(target, next) { var objList = targetObjsMap[target[relation.keyTo].toString()]; async.each(objList, function (obj, next) { + if (!obj) return next(); obj.__cachedRelations[relationName].push(target); processTargetObj(obj, next); }, next); @@ -528,11 +530,11 @@ Inclusion.include = function (objects, include, options, cb) { function linkManyToOne(target, next) { //fix for bug in hasMany with referencesMany var targetIds = [].concat(target[relation.keyTo]); - async.each(targetIds, function (targetId, proceed) { + async.each(targetIds, function (targetId, next) { var obj = objIdMap[targetId.toString()]; - if (!obj) return proceed(); + if (!obj) return next(); obj.__cachedRelations[relationName].push(target); - processTargetObj(obj, proceed); + processTargetObj(obj, next); }, next); } } @@ -620,6 +622,7 @@ Inclusion.include = function (objects, include, options, cb) { function linkOneToMany(target, next) { var objList = targetObjsMap[target[relation.keyTo].toString()]; async.each(objList, function (obj, next) { + if (!obj) return next(); obj.__cachedRelations[relationName] = target; processTargetObj(obj, next); }, next); @@ -681,7 +684,9 @@ Inclusion.include = function (objects, include, options, cb) { async.each(targets, linkOneToOne, next); function linkOneToOne(target, next) { var sourceId = target[relation.keyTo]; + if (!sourceId) return next(); var obj = objIdMap[sourceId.toString()]; + if (!obj) return next(); obj.__cachedRelations[relationName] = target; processTargetObj(obj, next); } @@ -748,6 +753,7 @@ Inclusion.include = function (objects, include, options, cb) { var targetId = target[relation.keyTo]; var objList = objTargetIdMap[targetId.toString()]; async.each(objList, function (obj, next) { + if (!obj) return next(); obj.__cachedRelations[relationName] = target; processTargetObj(obj, next); }, next);