loopback - Loopback 3:一个模型上的多个 HasOne 关系
问题描述
所以,我打开了她的一个问题,因为在我看来它应该像我想的那样工作......但可能是错误的,所以寻找另一种方式
所以,我几乎有两个模型,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 获得相同的两条记录?
解决方案
不要使用“hasOne”,而是使用“belongsTo”。
推荐阅读
- spring-mvc - Spring-MVC Oracle 数据源连接问题
- node.js - 面向对象编程的表达
- maven-plugin - 在 Eclipse IDE 中运行我的 TestNG 时出现错误,任何人都可以看看
- javascript - 在网站上实现 JS 计数效果
- python - Python 尝试并排除混乱
- windows - 将密钥注入 Windows 上不同应用程序的通用解决方案?
- sql - 在查询中减去 2 个给定类型的两列,否则保持原样
- reactjs - 如何使用 React 钩子在动态事件处理程序中增加值?
- https - Gravatar 默认位置因丢失电子邮件而错误重定向到 https://i*.wp.com/
- atom-editor - 无法加载 Atom-live-server