环回关系未填充对象ID的数组

时间:2018-08-17 00:49:21

标签: mongodb loopbackjs

到目前为止,我有2种模型:工作流程核心工作流程步骤'

工作流核心具有一个step属性,该属性的类型为array,包含1个多个步骤。在 workflow-core 上调用get时,响应正文未使用实际的step对象填充step数组。

workflow-core.json:

letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
chars = [0 for _ in range(len(letters))]

workflow-step.json:

{
"name": "workflow-core",
"base": "PersistedModel",
"idInjection": true,
"options": {
  "validateUpsert": true
},
"injectOptionsFromRemoteContext": true,
"properties": {
  "name": {
    "type": "string",
    "required": true,
    "default": "Untitled"
  },
  "user_id": {
    "type": "string",
    "required": false
  },
  "steps": {
    "type": "array",
    "required": false,
    "default": []
  }
},
"validations": [],
"relations": {
  "steps": {
    "model": "workflow-step",
    "type": "embedsMany"
  }
},
"acls": [
  {
    "accessType": "*",
    "principalType": "ROLE",
    "principalId": "$unauthenticated",
    "permission": "DENY"
  }
],
"methods": {}
}

Loopback Explorer说我应该得到的(以及我想要的):

{
"name": "workflow-step",
"base": "PersistedModel",
"idInjection": true,
"options": {
  "validateUpsert": true
},
"properties": {
  "name": {
    "type": "string",
    "required": true,
    "default": "Untitled Step"
  },
  "status": {
    "type": "string",
    "default": "waiting"
  }
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

我得到的是什么

{
  "name": "Untitled",
  "user_id": "string",
  "steps": [],
  "id": "string",
  "workflow-steps": [
    {
      "name": "Untitled Step",
      "status": "waiting",
      "id": "string"
    }
  ]
}

hasMany似乎做同样的事情,除了响应正文中没有工作流步骤数组属性

2 个答案:

答案 0 :(得分:0)

这最终很容易解决:

  1. 我在workflow-core.json中更改了步骤关系:

    "relations": {
        "steps": {
           "type": "hasMany",  <-- used hasMany instead of embedsMany
           "model": "WorkflowStep",
           "foreignKey": ""
         }
    },
    
  2. 使用api资源管理器窗口时,我需要添加过滤器:{“ include”:“ steps”}

enter image description here

不确定这是否是其中的一部分,但我按如下方式更改了模型名称:

workflow-core ---> WorkflowCore
工作流程步骤---> WorkflowStep

答案 1 :(得分:0)

下面给出了对我有用的内容。如果属性中有一组 ID,请使用 referencesMany 关系类型。

模型 A:

{
    id: 100,
    bIdList: [200, 201]
}

模型 B:

{
    id: 200,
    active: true
},
{
    id: 201,
    active: false
}

记住 bIdList 应该是 ObjectID 的数组,而不是字符串。

模型A中添加与模型B的关系如下:

"relations": {
    "bRelation": {
        "type": "referencesMany",
        "model": "B",
        "foreignKey": "bIdList"
     }
},

对于如下所示的查找查询,

modelA.findOne({include: 'bRelation'});

它会返回这样的结果,

{
    id: 100,
    bIdList: [200, 201]
    bRelation: [
        {
            id: 200,
            active: true
        },
        {
            id: 201,
            active: false            }
    ]
}

链接到详细说明相同内容的 Loopback 3 文档。 https://loopback.io/doc/en/lb3/Embedded-models-and-relations.html#referencesmany