【问题标题】:How to filter related models (hasMany relation) using where in LoopBack 3如何在 LoopBack 3 中使用 where 过滤相关模型(hasMany 关系)
【发布时间】:2022-05-11 19:23:22
【问题描述】:

客户与工作有很多关系

按顶级属性过滤的工作原理如下所示

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" } ]

【问题讨论】:

    标签: javascript arrays node.js loopbackjs loopback


    【解决方案1】:

    Customer.find({
      include: {
        relation: 'jobs',
        scope: {
          where: {
            jobType: 'Plumbing'
          }
        }
      }
    }, (err, data) => {
      // TODO handle error
      console.log(data)
    })

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      相关资源
      最近更新 更多