环回模型不更新 - 环回:错误:没有为ChatMessage模型定义关系“chatroomID”
问题描述:
我查看了文档和github问题。环回模型不更新 - 环回:错误:没有为ChatMessage模型定义关系“chatroomID”
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"]
}
}
}
})
答
在比迪烟的关系外键应该是相同的。
另请注意,您将关系模型设置为错误。这是ChatMessage
不ChatMessages
(的“s”) 变化是这样的:
//chat-room.json
{
"name": "ChatRoom",
"base": "PersistedModel",
"idInjection": true,
"options": {
"relations": {
"chatMessages": {
"type": "hasMany",
"model": "ChatMessage",
"foreignKey": "chatroomID"
}
}
},
...
但你得到的错误是因为包括没有定义关系,你没有chatroomID
关系。你有chatMessages
的关系。
因此改变它想:
ChatRoom.find({
"filter": {
"include": {
"relation": "chatMessages"
}
}
感谢您的答复。确实如此,仍然有错误:未处理的请求GET/ChatMessages错误?筛选器=%7B%22包括%22:%7B%22关系%22:%22chatMessages%22%7D%7D:错误:关系“chatMessages”不是定义为ChatMessage模型“# –
@NatuMyers对不起。我的错。被调用的模型是“ChatRoom”而不是“ChatMessage”。我更新了我的答案。这是代码中的另一个问题,你称错了模型 –
非常感谢你