在其中包含其中的回送查询

时间:2019-02-20 22:11:35

标签: javascript loopbackjs

所以我正尝试通过以下方式查询我的数据:

const filter = {where: {activity_id: activity_id, include: {relation: 'planGoal', scope: {where: {activity_goal_id: goal_id}}}}};

但是,这似乎不起作用,仅应用了activity_id过滤器,并返回了错误的数据。

所以我的问题是如何查询包含中的数据?甚至有可能吗?

以下是供参考的模型:

    {
  "name": "plan_goal_has_action_option",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "activity_id": {
      "type": "number"
    },
    "plan_has_goal_id": {
      "type": "number"
    },
    "action_option": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "planGoal": {
      "type": "belongsTo",
      "model": "plan_has_goal",
      "foreignKey": "plan_has_goal_id"
    }
  },
  "acls": [],
  "methods": {}
}


    {
  "name": "plan_has_goal",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "activity_id": {
      "type": "number"
    },
    "activity_goal_id": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {
    "goal": {
      "type": "belongsTo",
      "model": "activity_goal",
      "foreignKey": "activity_goal_id"
    },
    "actions": {
      "type": "hasMany",
      "model": "plan_goal_has_action_option",
      "foreignKey": "plan_has_goal_id"
    }
  },
  "acls": [],
  "methods": {}
}

1 个答案:

答案 0 :(得分:1)

includewhere是两个不同的过滤器:

{  
  where: {
    activity_id: activity_id
  },
  include: {  
    relation: 'planGoal',
    scope: {
      where: {
        activity_goal_id: goal_id
      }
    }
  }
}