如何从include模型中过滤回送字段?

时间:2017-05-01 08:55:13

标签: angularjs loopbackjs

我有这个问题:

UserModel.find({

    filter: {
        include: 
        [
            "communications",
             "roles"
        ],
        where : {
            "object_type" : 1
        }
    } 
}, function(data){
    $rootScope.users = data;
});

我希望通过字段" object_type"从通信模型过滤器中获取所有数据。但它并没有像我想的那样工作。

My Communications模型如下所示:

...
"properties": {
"object_type": {
  "type": "number"
},
"object_id": {
  "type": "number"
},
"communications_type_code": {
  "type": "number"
},
"address_type": {
  "type": "number"
},
"contact_value": {
  "type": "string"
},
"notes": {
  "type": "string"
},
....

2 个答案:

答案 0 :(得分:1)

要查询相关模型,您必须使用关系范围。在你的情况下,这将是这样的:

UserModel.find({
    filter: {
        include: 
        [
            {
              "relation": "communications",
              "scope": { 
                "where": {"object_type": 1}
              }

            },
            "roles"
        ]
    } 
}, function(data){
    $rootScope.users = data;
});

查看有关"Querying related models"

的文档

答案 1 :(得分:0)

Oded,我相信你不需要搜索参数中的filter属性。它应该是这样的:

UserModel.find({
    include: [ "communications", "roles" ],
    where : { "object_type" : 1 }
}, function(data){
    $rootScope.users = data;
});

还要确保添加到include的其他实体与查询的模型有关系。它真的是UserModel还是应该是CommunicationsModel