I have one model related to another with a hasMany relation. I am using the include filter to get back a
javascript structure which should have an array objects of the first model type each of which should have a member array that lists the related model objects:
{ include: { relation: 'cssrules' }}
When I do a console.log(result) I see that it appear to be correct. I have an array of the first model, and each of those has a member which is cssrules that is an array of objects.
If I do a console.log(JSON.stringify(result)) I see that expands out the object references in the cssrules array and correctly displays all of the information.
But when I try to accesses the structure like:
result[i].cssrules.length
or
result[i].cssrules[j]
I get errors about cssrules being undefined.
If I do the following:
var tmp = JSON.stringify(result);
result=JSON.parse(tmp);
then it seems to behave correctly, but this seems wasteful and unnecessary. Why can't I access the
initial structure returned to me?
@jdschreck I am trying to look into this issue, could you fork sandbox: https://github.com/strongloop/loopback-sandbox and replace with your testing?
Thanks.
I will give that a try.
John
From: Janny [email protected]
To: strongloop/loopback [email protected]
Cc: John Schreck/Fishkill/IBM@IBMUS
Date: 04/06/2016 02:23 PM
Subject: Re: [strongloop/loopback] Trouble accessing javascript
structure returned by find function with include filter (#2205)
@jdschreck I am trying to look into this issue, could you fork sandbox:
https://github.com/strongloop/loopback-sandbox and replace with your
testing?
Thanks.
?
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
I have created https://github.com/jdschreck/loopback-sandbox which demonstrates the problem.
To run this testcase in the Explorer execute the following urls:
Model1/init
Model2/init
Model1/runQuery
There are two files present... bad.output which exhibits the problem and good.output which shows it working if I do a parse(stringify(results)). The example is configured to produce the bad output initially.
If you then uncomment line 49 of model-1.js it will run successfully.
@jdschreck Thank you for the extreme detailed sandbox to reproduce the problem.
So what happens here is when you get the raw data, the type of the included data is Function instead of Object.
You can verify this by
console.log("Here is the console.log on the raw result:");
console.log(result[0].model2s);
console.log("data type of raw result");
console.log(typeof result[0].model2s);
console.log("Here is the console.log on the parsed result:");
console.log(result[0].model2s);
console.log("data type of parsed result");
console.log(typeof result[0].model2s);
Console.log result:
Here is the console.log on the raw result:
{ [Function]
_receiver:
{ description: 'description1',
catalog: 'catalog1',
name: 'MyFirstModel1',
model2s: List [ [Object], [Object], [Object] ] },
_scope: { where: { test1: 'MyFirstModel1' } },
_targetClass: 'Model2',
getAsync: [Function],
build: [Function: bound ],
create: [Function: bound ],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function: bound ],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function: bound ],
updateById: [Function: bound ],
exists: [Function: bound ] }
data type of raw result
function
Here is the console.log on the parsed result:
[ { name: 'model2_instance3',
test2: 'pqr',
test1: 'MyFirstModel1' },
{ name: 'model2_instance4',
test2: 'tuv',
test1: 'MyFirstModel1' },
{ name: 'model2_instance5',
test2: 'wxy',
test1: 'MyFirstModel1' } ]
data type of parsed result
object
I cannot say is it a bug or designed in this way for other purposes at this moment. Will dig into it then update here.
Since JSON.Stringify(result) then JSON.parse(result) can convert the data from function to object, if you need to process the data please use it as a workaround. If it is for some reason designed as this, I will add this approach to doc.
Janny,
Thanks for looking into this. I look forward to hearing what more you
find out. It is unclear to me how console.log and JSON.stringify seem to
be successful in navigating this structure to print out what we expect to
see.
John
From: Janny [email protected]
To: strongloop/loopback [email protected]
Cc: John Schreck/Fishkill/IBM@IBMUS
Date: 04/18/2016 05:30 PM
Subject: Re: [strongloop/loopback] Trouble accessing javascript
structure returned by find function with include filter (#2205)
@jdschreck Thank you for the extreme detailed sandbox to reproduce the
problem.
So what happens here is when you get the raw data, the type of the
included data is Function instead of Object.
You can verify this by
console.log("Here is the console.log on the raw result:");
console.log(result[0].model2s);
console.log("data type of raw result");
console.log(typeof result[0].model2s);
console.log("Here is the console.log on the parsed result:");
console.log(result[0].model2s);
console.log("data type of parsed result");
console.log(typeof result[0].model2s);
Console.log result:
Here is the console.log on the raw result:
{ [Function]
_receiver:
{ description: 'description1',
catalog: 'catalog1',
name: 'MyFirstModel1',
model2s: List [ [Object], [Object], [Object] ] },
_scope: { where: { test1: 'MyFirstModel1' } },
_targetClass: 'Model2',
getAsync: [Function],
build: [Function: bound ],
create: [Function: bound ],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function: bound ],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function: bound ],
updateById: [Function: bound ],
exists: [Function: bound ] }
data type of raw result
function
Here is the console.log on the parsed result:
[ { name: 'model2_instance3',
test2: 'pqr',
test1: 'MyFirstModel1' },
{ name: 'model2_instance4',
test2: 'tuv',
test1: 'MyFirstModel1' },
{ name: 'model2_instance5',
test2: 'wxy',
test1: 'MyFirstModel1' } ]
data type of parsed result
object
I cannot say is it a bug or designed in this way for other purposes. I
will dig into it then update here.
?
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
@jdschreck
The name is returned by this function: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/scope.js#L176
That's why its type is function.
Please check comment https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/scope.js#L162-L169 to see details
Janny,
Sorry that answer is a little too terse for me to understand. Can you
tell me how I can navigate the returned structure? I don't understand why
JSON.stringify does not have trouble navigating the structure, but I do.
Thanks,
John
From: Janny [email protected]
To: strongloop/loopback [email protected]
Cc: John Schreck/Fishkill/IBM@IBMUS, Mention
[email protected]
Date: 04/28/2016 03:31 PM
Subject: Re: [strongloop/loopback] Trouble accessing javascript
structure returned by find function with include filter (#2205)
@jdschreck
The name is returned by this function:
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/scope.js#L176
That's why it's type is function.
Please check comment
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/scope.js#L162-L169
to see details
?
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
@jdschreck this property name is defined by Object.defineProperty(), so the value is returned by get. When you apply JSON.stringify to it, JSON.stringify will get value by function get.
Is it clear now?
Janny,
This is unfortunately not clear to me yet. Is there a way to access the returned structure to access the list of model2s without parsing/stringifying?
John
Hello,
If i may, you can console.log the item.relation and discover the schema.
More specific if you use the .getAsync() method you will get a promise with the data.
Item.relationProp.getAsync().then((rel)=>{ //do stuff with 'rel' })
I ran into a similar problem with a findById with multiple relations and sub relations. For what it's worth, JSON.parse(JSON.stringify(result)) seemed to work for me.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.
+1 im getting this too when trying to. And landed here after an hour of scratching my head and trying out all different combinations of for and while and forEach loops and after then consoling the weird list
+1 Also ran into this issue.
+1 also having trouble with this.
Neither JSON.parse(JSON.stringify(estudante.turma)) or toJSON() are working
First one throws "Unexpected token u in JSON"
Second one throws toJSON is not a function
Most helpful comment
Janny,
This is unfortunately not clear to me yet. Is there a way to access the returned structure to access the list of model2s without parsing/stringifying?
John