mongodb - 环回:关系通过 - 不工作
问题描述
所以,我被困在一个问题上,这应该很简单,我确信我遗漏了一些明显的东西
我正在关注此文档:
所以我有 3 张桌子
客户,团队,客户团队
客户端.json
{
"name": "client",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"teams": {
"type": "hasMany",
"model": "team",
"foreignKey": "teamId",
"through": "client-team"
}
},
"acls": [],
"methods": {}
}
团队.json
{
"name": "team",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"type": {
"type": "string",
"required": true,
"default": "first-team"
},
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"clients": {
"type": "hasMany",
"model": "client",
"foreignKey": "clientId",
"through": "client-team"
}
},
"acls": [],
"methods": {}
}
客户端-team.json
{
"name": "client-team",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"clientId": {
"type": "string",
"required": true
},
"teamId": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"client": {
"type": "belongsTo",
"model": "Client",
"foreignKey": "clientId"
},
"team": {
"type": "belongsTo",
"model": "Team",
"foreignKey": "teamId"
}
},
"acls": [],
"methods": {}
}
所以所有的关系都设置正确(我认为)......
然后在我的客户中,我确实有 1 个客户
[
{
"name": "Client name",
"id": "59876185508eb519385779c6"
}
]
在我的团队中,我有很多,但可以肯定的是:
[
{
"type": "type",
"name": "Team name",
"id": "5ae8a37add2989a32d37f83d"
}
]
然后我去我的
本地主机:3000/资源管理器
发布客户团队
像这样
{
"clientId": "59876185508eb519385779c6",
"teamId": "5ae8a37add2989a32d37f83d"
}
我得到了 200 响应:
{
"clientId": "59876185508eb519385779c6",
"teamId": "5ae8a37add2989a32d37f83d",
"id": "5ae961873a7e3b33f0579fc3"
}
所以连接就在那里....
但是,当我去“GET client/id”并做
id: 59876185508eb519385779c6 过滤器: {"include":["teams"]}
这是回应
{
"name": "Chelsea FC",
"id": "59876185508eb519385779c6",
"teams": []
}
在“GET teams/id”中也会发生同样的情况,我使用
id: 5ae8a37add2989a32d37f83d 过滤器: {"include":["clients"]}
或者如果我去“GET teams/{id}/clients”并输入 id: 5ae8a37add2989a32d37f83d
我明白了
[]
那么我做错了什么?我确信我错过了一个愚蠢的、明显的事情:/
如果这有什么不同,请使用 mongo
解决方案
这里有三个问题:
- 您将 mongodb 标识符描述为字符串,这就是为什么您将字符串存储在数据库中而不是对象 ID 中的原因。(这不是必需的,因为数据源应该了解真实类型)
- 您的模型从小写字母开始。关系中也应该如此。(问题的第一部分,它正在解决 ids 的问题)
- 客户和团队模型的关系不正确(问题的第二部分,它正在修复包括)
客户端-team.json
{
"name": "client-team",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"clientId": {
"type": "objectId", // !!! changed (not required)
"required": true
},
"teamId": {
"type": "objectId", // !!! changed (not required)
"required": true
}
},
"validations": [],
"relations": {
"client": {
"type": "belongsTo",
"model": "client", // !!! changed
"foreignKey": "clientId"
},
"team": {
"type": "belongsTo",
"model": "team", // !!! changed
"foreignKey": "teamId"
}
},
"acls": [],
"methods": {}
}
客户端.json
{
"name": "client",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"teams": {
"type": "hasMany",
"model": "team",
"foreignKey": "clientId", // !!! changed (we describing id of this model, not team)
"through": "client-team"
}
},
"acls": [],
"methods": {}
}
团队.json
{
"name": "team",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"type": {
"type": "string",
"required": true,
"default": "first-team"
},
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"clients": {
"type": "hasMany",
"model": "client",
"foreignKey": "teamId", // !!! changed (the same as the previous)
"through": "client-team"
}
},
"acls": [],
"methods": {}
}
推荐阅读
- python - 如何从三个数组创建 x、y、z 坐标,其中 x 和 y 是使用 meshgrid 生成的,而 z 取决于 x?
- jquery - $.each + $.get/$.ajax 没有得到正确的数据
- ansible - how to enable openstack_inventory.py caching?
- c++ - Semi tower of hanoi
- java - Spring Boot apps starting up in docker container having trouble connecting to spring cloud config server that is also running in docker container
- tensorflow - IndexError: index 5407 is out of bounds for axis 1 with size 8
- javascript - 如何使用javascript动态添加区域标签以映射标签
- php - Wrong addition of hours to a MySQL table column timestamp Laravel
- python - 大张量的有效路径更新
- reactjs - How to test that a media query css applies to an element upon screen resize using jest and enzyme in reactjs