我查看了文档和github问题。
https://loopback.io/doc/en/lb2/HasMany-relations.html
https://github.com/strongloop/loopback-datasource-juggler/issues/76
hasMany relation: including from the other direction
我无法理解为什么我收到错误:Error: Relation "chatroomID" is not defined for ChatMessage model
似乎即使我正确编辑了我的json,我的聊天室模型也没有更新(如REST浏览器中所示)
但是chatmessage确实设法更新了
聊天的message.json
{
"name": "ChatMessage",
"base": "PersistedModel",
"idInjection": true,
"options": {
"relations": {
"ChatRoom": {
"type": "belongsTo",
"model": "ChatRoom",
"foreignKey": "chatroomID"
},
...
聊天room.json
{
"name": "ChatRoom",
"base": "PersistedModel",
"idInjection": true,
"options": {
"relations": {
"chatMessages": {
"type": "hasMany",
"model": "ChatMessages",
"foreignKey": "chatMessagesID"
}
}
},
...
在控制器中:
function getMsgs() {
// http://loopback.io/doc/en/lb2/Include-filter.html
return (
ChatMessage.find({
"filter": {
"include": {
"relation": "chatroomID",
"scope": {
"include": ["ChatRoom"]
}
}
}
})
答案 0 :(得分:3)
在双向关系中,外键应该相同。
另请注意,您将关系模型设置为错误。它是ChatMessages
而非//chat-room.json
{
"name": "ChatRoom",
"base": "PersistedModel",
"idInjection": true,
"options": {
"relations": {
"chatMessages": {
"type": "hasMany",
"model": "ChatMessage",
"foreignKey": "chatroomID"
}
}
},
...
('s')
改变如下:
chatroomID
但是你得到的错误是因为包含未定义的关系,你没有chatMessages
关系。您有ChatRoom.find({
"filter": {
"include": {
"relation": "chatMessages"
}
}
关系。
所以改变它:
{{1}}