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

Loopback relations not populating array of Object IDs

到目前为止我有 2 个模型:workflow-core,workflow-step'

工作流核心有一个步骤 属性,它是数组类型并包含 1 个步骤。在 workflow-core 上调用 get 时,响应主体未使用实际步骤对象填充步骤数组。

工作流程-core.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": {}
}

工作流程-step.json:

{
"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": {}
}

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

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

我得到了什么:

{
    "name": "Updated with step",
    "user_id": "231569461654",
    "id": "5b75bc769b3143103cb787c2",
    "workflow-steps": [],
    "steps": [
      "5b760fff24ccc23934fef240"
    ]
  }

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

这最终是一个简单的修复:

  1. 我修改了工作流中的步骤关系-core.json:

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

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

工作流核心 ---> 工作流核心
工作流步骤 ---> WorkflowStep

下面给出了对我有用的方法。如果 属性 中有 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'});

会return这样的结果,

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

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