Loopback 3:在一个模型上有多个HasOne关系

问题描述 投票:0回答:1

所以,我opened an issue her e,我认为它应该按照我的想法工作,但是可能是错误的,因此寻求另一种方法

所以,我几乎有两个模型,Wedding和Person。婚礼之一设置了以下关系:

"people": {
  "type": "hasMany",
  "model": "person",
  "foreignKey": "",
  "options": {
    "nestRemoting": true
  }
},
"partner1": {
  "type": "hasOne",
  "model": "person",
  "foreignKey": ""
},
"partner2": {
  "type": "hasOne",
  "model": "person",
  "foreignKey": ""
}

我的一个结婚证件看起来像这样(如果您不能告诉我,我正在使用mongoDB:]

{
  "_id": "5de78c76f89d1a8ad4091ca5",
  "date": "2019-12-04T10:37:42.000Z",
  "userId": "5de78c76f89d1a8ad4091ca4",
  "created": "2019-12-04T10:37:42.720Z",
  "partner1Id": "5de78c77f89d1a8ad4091ca6",
  "partner2Id": "5de78c77f89d1a8ad4091ca7"
}

所以,当我设置包括过滤器并执行:

{ "include": ["partner1", "partner2"]}

在我的环回API资源管理器中,http://localhost:3000/api/weddings/5de78c76f89d1a8ad4091ca5

我得到:

{
  "date": "2019-12-04T10:37:42.000Z",
  "id": "5de78c76f89d1a8ad4091ca5",
  "userId": "5de78c76f89d1a8ad4091ca4",
  "created": "2019-12-04T10:37:42.720Z",
  "partner1Id": "5de78c77f89d1a8ad4091ca6",
  "partner2Id": "5de78c77f89d1a8ad4091ca7",
  "partner1": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  },
  "partner2": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  }
}

但是,我期望这样:

{
  "date": "2019-12-04T10:37:42.000Z",
  "id": "5de78c76f89d1a8ad4091ca5",
  "userId": "5de78c76f89d1a8ad4091ca4",
  "created": "2019-12-04T10:37:42.720Z",
  "partner1Id": "5de78c77f89d1a8ad4091ca6",
  "partner2Id": "5de78c77f89d1a8ad4091ca7",
  "partner1": {
    "id": "5de78c77f89d1a8ad4091ca6",
    "fullName": "Michael Knight",
    "spouse": "spouse1",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  },
  "partner2": {
    "id": "5de78c77f89d1a8ad4091ca7",
    "fullName": "Jessica Alba",
    "spouse": "spouse2",
    "contacts": [],
    "verified": false,
    "created": "2019-12-04T10:37:43.292Z",
    "updated": "2019-12-04T10:37:43.292Z",
    "userId": "5de78c76f89d1a8ad4091ca4",
    "weddingId": "5de78c76f89d1a8ad4091ca5"
  }
}

为什么有任何想法?为什么我得到partner1和partner2相同的两个记录?

loopback
1个回答
0
投票

而不是使用“ hasOne”,而是使用“ belongsTo”。

© www.soinside.com 2019 - 2024. All rights reserved.