From 0e7acec591f8b8c5dd6c48316af84e6fbfe6272e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sun, 23 Mar 2014 21:07:04 -0700 Subject: [PATCH] Set the relation property correctly See https://github.com/strongloop/loopback/issues/218 --- lib/include.js | 13 +++++++++++-- test/include.test.js | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/include.js b/lib/include.js index 5a0bb062..e044f062 100644 --- a/lib/include.js +++ b/lib/include.js @@ -110,7 +110,11 @@ Inclusion.include = function (objects, include, cb) { defineCachedRelations(obj); // Set to null if the owner doesn't exist obj.__cachedRelations[relationName] = null; - obj[relationName] = null; + if(obj === inst) { + obj.__data[relationName] = null; + } else { + obj[relationName] = null; + } return callback(); } } @@ -123,7 +127,12 @@ Inclusion.include = function (objects, include, cb) { } else { defineCachedRelations(obj); obj.__cachedRelations[relationName] = result; - obj[relationName] = result; + if(obj === inst) { + obj.__data[relationName] = result; + obj.strict = false; + } else { + obj[relationName] = result; + } if (subInclude && result) { var subItems = relation.multiple ? result : [result]; diff --git a/test/include.test.js b/test/include.test.js index bd918d5d..72980903 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -70,6 +70,7 @@ describe('include', function () { should.exist(user); user.id.should.equal(p.ownerId); user.__cachedRelations.should.have.property('posts'); + user.should.have.property('posts'); user.__cachedRelations.posts.forEach(function (pp) { pp.userId.should.equal(user.id); }); @@ -97,6 +98,7 @@ describe('include', function () { user.__cachedRelations.should.have.property('posts'); user.__cachedRelations.posts.forEach(function (pp) { pp.userId.should.equal(user.id); + pp.should.have.property('author'); pp.__cachedRelations.should.have.property('author'); var author = pp.__cachedRelations.author; author.id.should.equal(user.id);