Loopback Models Not Updating - Loopback: Error: Relation "chatroomID" is not defined for ChatMessage model

Loopback Models Not Updating - Loopback: Error: Relation "chatroomID" is not defined for ChatMessage model

我查看了文档和 github 个问题。

https://loopback.io/doc/en/lb2/HasMany-relations.html

https://github.com/strongloop/loopback-datasource-juggler/issues/76

我不知道为什么会出现错误:Error: Relation "chatroomID" is not defined for ChatMessage model

似乎即使我正确编辑了 json,我的聊天室模型也没有更新(如 REST 浏览器中所示)

但聊天消息确实成功更新了


聊天-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"
        }
      }