loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Can't access included relations' properties

Open luisfavila opened this issue 9 years ago • 2 comments

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]

luisfavila avatar Apr 10 '17 15:04 luisfavila

#2205 Related

luisfavila avatar Apr 10 '17 15:04 luisfavila

@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

padelagani avatar Apr 24 '17 05:04 padelagani