Loopback: Can't access included relations' properties

Created on 10 Apr 2017  路  2Comments  路  Source: strongloop/loopback

Hi! I'm struggling a bit with an include behavior I wouldn't expect.
I have a model A which has a belongsTo relation with model B.

A.findById(id, {"include":"b"}, function(err, data){
    console.log(data);
    console.log(data.b);
})

Description/Steps to reproduce

I have a model A which has a belongsTo relation with model B.

A.findById(id, {"include":"b"}, function(err, data){
    console.log(data);
    console.log(data.b);
})

Expected result

On the first console.log I can actually see something like { b: { ... }, .... }, and it's properties but the second one will get me:

{ [Function: bound ]
getAsync: [Function: bound ],
update: [Function: bound ],
destroy: [Function: bound ],
create: [Function: bound ],
build: [Function: bound ],
_targetClass: 'B' }

Then if I try to console.log(data.b.xproperty) it will just return undefined when it was clearly shown on the console.log(data)

Additional information

linux x64 7.4.0
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]

stale

Most helpful comment

@luisfavila Have a look on this - https://loopback.io/doc/en/lb3/Include-filter.html#access-included-objects

Try to do - data.toJSON() then access b

All 2 comments

2205 Related

@luisfavila Have a look on this - https://loopback.io/doc/en/lb3/Include-filter.html#access-included-objects

Try to do - data.toJSON() then access b

Was this page helpful?
0 / 5 - 0 ratings