Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter related models (hasMany relation) using where #4257

Closed
me1go opened this issue Sep 24, 2019 · 4 comments
Closed

How to filter related models (hasMany relation) using where #4257

me1go opened this issue Sep 24, 2019 · 4 comments
Labels

Comments

@me1go
Copy link

me1go commented Sep 24, 2019

Customer hasMany relationship with jobs

Filtering by the top-level property works as shown below

Customer.find({ include: ["jobs"],where :{username:"mel"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

But I am unable to filter by a second-level property which has nested data.

I tried the below it but doesn't seem to be working
Customer.find({ include: ["jobs"],where :{"jobs.JobTypes":"Plumbing"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

I have tried this as well.
Customer.find({ include: ["jobs"],where :{"jobs":{"JobTypes":"Plumbing"}} }, function(err, data) {
console.log(data);
cb(null, true, data);
});

This is the json I am trying to Filter
{ [ { "customer_id": 1, "customer_name": "1", "realm": "string", "username": "mel", "email": "[email protected]", "emailVerified": false, "id": "5d88ac24a823fa5504b2db1f", "jobs": [ { "jobId": 1, "CustomerId": 1, "jobType": "Painting", "id": "5d88c4670f527a484c57b09d" }, { "jobId": 2, "CustomerId": 1, "jobType": "Plumbing", "id": "5d88c47f0f527a484c57b09e" }, { "jobId": 3, "CustomerId": 1, "jobType": "Dancing", "id": "5d88c4920f527a484c57b09f" } ]

@7ranveer
Copy link

7ranveer commented Oct 1, 2019

Below Code might be useful

Customer.find({ where :{username:"mel"}, include: [{ relation: 'jobs', scope: { where: { JobTypes: 'Plumbing' } } }]}, function(err, data) { console.log(data); cb(null, true, data); });

@agnes512
Copy link
Contributor

@melGO44 We don't support scope for inclusion in LB4 yet if you're using LB4.

@stale
Copy link

stale bot commented Dec 23, 2019

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.

@stale stale bot added the stale label Dec 23, 2019
@agnes512
Copy link
Contributor

We support custom scope for inclusion now loopbackio/loopback-next#3453 :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants