Minimal repo with which to reproduce bug:
https://github.com/KevinMongiello/loopback_include_bug
Steps to reproduce:
0a) Run mongodb
0b) Open up explorer http://localhost:3333/explorer
1) Create/Post a new author,
2) Create/Post a new 'post'
3) Open "GET" in author section
4) Paste this in filter {"include": {"relation": "post", "scope": {"fields": "id"}} }
5) hit enter / try it out
Loopback will crash with TypeError: Cannot read property 'toString' of undefined
Expected result is to return all authors with a property posts that only includes each post's id.
Actual result: Loopback crashes.
darwin x64 6.2.0
[email protected] /Users/kmong/CaliberMind/Work/scrap/bugs/loopback_include_bug
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
@KevinMongiello nice job with the sample repository! I am able to see the error. let me take a look at what is failing.
@KevinMongiello This would need a fix, I have temporary solution for the issue, if you query with {"include": {"relation": "post", "scope": {"fields": ["id", "authorId"]}} } the error wouldnt occur
@bajtos @superkhau the following line creates a filter with only the query fields {'post': {fields: 'id'}}, but does not add relation.keyFrom : authorId
https://github.com/strongloop/loopback-datasource-juggler/blob/76c73d55d28dfa150b9e004aeae12aafe0ce6475/lib/include.js#L311
so , the data retrieved does not have the authorId and later at line https://github.com/strongloop/loopback-datasource-juggler/blob/76c73d55d28dfa150b9e004aeae12aafe0ce6475/lib/include.js#L599 ,
we try to merge the relations {'post'} with the parent 'author' using authorId and it throws an error and crashes loopback
I'm not familiar enough with this area of code, @raymondfeng can you chime in here?
@bajtos @superkhau @raymondfeng @jannyHou : I reported this issue a while ago in the datasource-juggler:
Please see https://github.com/strongloop/loopback-datasource-juggler/issues/779 and https://github.com/strongloop/loopback-datasource-juggler/issues/890
For now you need to manually include the foreign key in the scope of the incriminated relation for it to work, otherwise it crashes the app
I've found that {"include": {"relation": "post", "scope": {"fields": {"id": "true"} } } } works, and I am successfully using this as a solution right now. @deepakrkris @ebarault
Thank you @ebarault for chiming in. I am closing this issue as a duplicate of strongloop/loopback-datasource-juggler#779 then.
Most helpful comment
I've found that
{"include": {"relation": "post", "scope": {"fields": {"id": "true"} } } }works, and I am successfully using this as a solution right now. @deepakrkris @ebarault