javascript - 如何使用LoopBack 3中的where过滤相关模型(hasMany关系)

标签 javascript arrays node.js loopbackjs loopback

客户与工作有很多关系

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

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/

相关文章:

javascript - 如何使用 javascript 或 jQuery 在 Microsoft Edge 中禁用 "swipe to go back"?

c++ - 关于 C++ 的简单一般问题

arrays - Excel VBA : Element Not Being Stored in Loop

Javascript Promise 模式 - 区分错误

javascript - 在 Flow 中定义子类型的正确方法是什么?

javascript - 如何重新加载页面(AngularJs)

javascript - 创建不能选择过去的日子,只能选择一定数量的 future 日子的日历

java - 从 JMenuBar 获取 JMenuItems

node.js - 为 React Native 安装 Node.js

javascript - 我如何在 dynamicHepler 中渲染一个部分?