loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Include relation filter crashes loopback

Open KevinMongiello opened this issue 9 years ago • 7 comments

Description/Steps to reproduce

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

Expected result is to return all authors with a property posts that only includes each post's id. Actual result: Loopback crashes.

Additional information

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 avatar Feb 24 '17 22:02 KevinMongiello

@KevinMongiello nice job with the sample repository! I am able to see the error. let me take a look at what is failing.

deepakrkris avatar Feb 27 '17 18:02 deepakrkris

@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

deepakrkris avatar Feb 27 '17 22:02 deepakrkris

@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

deepakrkris avatar Feb 27 '17 22:02 deepakrkris

I'm not familiar enough with this area of code, @raymondfeng can you chime in here?

superkhau avatar Feb 27 '17 23:02 superkhau

@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

ebarault avatar Feb 28 '17 00:02 ebarault

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

KevinMongiello avatar Feb 28 '17 03:02 KevinMongiello

Thank you @ebarault for chiming in. I am closing this issue as a duplicate of strongloop/loopback-datasource-juggler#779 then.

bajtos avatar Mar 01 '17 09:03 bajtos