mean-expert-official / loopback-sdk-builder

Tool for auto-generating Software Development Kits (SDKs) for LoopBack
Other
399 stars 178 forks source link

Question : How to implement a nested query? #616

Closed PhilippeCorreges closed 5 years ago

PhilippeCorreges commented 5 years ago

What type of issue are you creating?

What version of this module are you using?

Write other if any:

Please add a description for your issue:

Hello, Would you please excuse me if my question seems to easy for some. For me this is a nightmare. Let's say I have to models, A and B. A is containing a foreign key from B (relation has many). as, every request is returning an Observable, how would I access a "field" of B ? Let's say I am having an Observable after a Find ''. On each result row, I have the ID of a row in B. I want to access another column of B according to the id I got in my first find ''.

Problem: I cannot include multiple subscribes it seems as this generated hundreds of requests.

Any help is appreciated.

Thanks!

Nosfistis commented 5 years ago

This seems more like a StackOverflow question...

You can try including the relation, using scope on relation etc as described in https://loopback.io/doc/en/lb3/Include-filter.html#include-with-filters

PhilippeCorreges commented 5 years ago

Unfortunately, this is precisely linked to how the SDK is generated. Maybe does it expose the proper method or did I miss something?

On StackOverflow, I do not have any option to create the new TAG called loopback-sdk-builder. I need 1500 reputation. Here is the question link: https://stackoverflow.com/questions/52274917/how-to-implement-a-nested-query-using-loopback-sdk-builder

PhilippeCorreges commented 5 years ago

Here is my ts code: this.souscription.find({include: {relation: 'category'} }) // this.userService.getSubscriptions(this.currentUser.id) .subscribe((tableData) => { this.dataSource.data = tableData; });

with Subscription having a BelongTo relation to Category: { "name": "Subscription", "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true }, "properties": { "subscriptionName": { "type": "string", "required": true }, "subscriptionDesc": { "type": "string", "required": false }, }, "validations": [], "relations": { "category": { "type": "belongsTo", "model": "Category", "foreignKey": "", "options": { "nestRemoting": true } }

tableData value does not contain anything linked to category except the id stored in Subscription...

PhilippeCorreges commented 5 years ago

OK. The problem was coming apparently from one Subscription did not have any id. This is the one I checked. So everything works fine.

This is a final update for possible people asking same things.

Thanks