【问题标题】:Loopback relations not populating array of Object IDs环回关系未填充对象 ID 数组
【发布时间】:2023-04-02 19:41:01
【问题描述】:

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

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

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

workflow-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 似乎做了同样的事情,只是响应正文中没有工作流步骤数组属性

【问题讨论】:

    标签: mongodb loopbackjs


    【解决方案1】:

    这最终很容易解决:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      模型 A:

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

      模型 B:

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

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

      Model A 中添加与 Model 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-03
        • 2014-03-07
        • 1970-01-01
        • 2019-05-16
        • 2020-11-11
        • 2020-11-24
        • 2013-03-10
        相关资源
        最近更新 更多