客户与工作有很多关系
按顶级属性进行过滤的工作原理如下所示
Customer.find({ include: ["jobs"],where :{username:"mel"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});
但我无法按具有嵌套数据的二级属性进行过滤。
我尝试了下面的方法,但似乎不起作用
Customer.find({ include: ["jobs"],where :{"jobs.JobTypes":"Plumbing"} }, function(err, data) {
console.log(data);
cb(null, true, data);
});
我也尝试过这个。
Customer.find({ include: ["jobs"],where :{"jobs":{"JobTypes":"Plumbing"}} }, function(err, data) {
console.log(data);
cb(null, true, data);
});
这是我要过滤的 json
{[{"customer_id": 1,
"customer_name": "1",
"realm": "string",
"username": "mel",
"email": "mel@gmail.com",
"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" } ]
最佳答案
Customer.find({
include: {
relation: 'jobs',
scope: {
where: {
jobType: 'Plumbing'
}
}
}
}, (err, data) => {
// TODO handle error
console.log(data)
})
关于javascript - 如何使用LoopBack 3中的where过滤相关模型(hasMany关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58078810/