【问题标题】:Loopback Models Not Updating - Loopback: Error: Relation "chatroomID" is not defined for ChatMessage model环回模型未更新 - 环回:错误:未为 ChatMessage 模型定义关系“chatroomID”
【发布时间】:2017-07-31 03:44:03
【问题描述】:

我查看了文档和 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 资源管理器中所示)

但聊天消息确实设法更新


聊天消息.json

{
  "name": "ChatMessage",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "relations": {
      "ChatRoom": {
        "type": "belongsTo",
        "model": "ChatRoom",
        "foreignKey": "chatroomID"
      },

...

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

})

【问题讨论】:

    标签: angularjs loopbackjs has-many strongloop relation


    【解决方案1】:

    在双向关系中,外键应该相同。

    还要注意你设置的关系模型错误。它是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"
            }
          }
    

    【讨论】:

    • 感谢您的回复。确实如此,仍然有错误:Unhandled error for request GET /ChatMessages?filter=%7B%22include%22:%7B%22relation%22:%22chatMessages%22%7D%7D: Error: Relation "chatMessages" is not defined for ChatMessage model
    • @NatuMyers 对不起。我的错。被调用的模型是ChatRoom 而不是ChatMessage。我更新了我的答案。这是您在错误模型上调用的代码中的另一个问题
    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多