【问题标题】:Loopbackjs: Followers and Followees Model (Twitter-like)Loopbackjs:关注者和关注者模型(类似 Twitter)
【发布时间】:2016-08-20 20:04:58
【问题描述】:

我想创建一个类似 Twitter 的模型,但无法使其工作:

到目前为止的用户模型:

 {
   "name": "NsUser",
   "base": "User",
 ...
    "relations": {
        "followers": {
          "type": "hasMany",
          "model": "User",
          "through": "Follow",
          "foreignKey": "followerUserId"
        },
        "followees": {
          "type": "hasMany",
          "model": "User",
          "through": "Follow",
          "foreignKey": "followeeUserId"
        }
    }
}

到目前为止关注的模型:

{
  "name": "Follow",
  "base": "PersistedModel",
...
  "relations": {
    "followee": {
      "type": "belongsTo",
      "model": "NsUser",
      "foreignKey": "followeeUserId"
    },
    "follower": {
      "type": "belongsTo",
      "model": "NsUser",
      "foreignKey": "followerUserId"
    }
  }
}

然后我在 Follow 模型中添加一个关系:

{
    "created": "2016-08-19T09:23:18.175Z",
    "id": "57b6d0068d55e5368538c8f4",
    "followeeUserId": "578f7ad2ba0bfc40d78ef985",
    "followerUserId": "5790bb3172e2738deb7532ee"
  }

但无法在http://0.0.0.0:3008/api/NsUsers/5790bb3172e2738deb7532ee/followers 中得到结果 这是崩溃:

{
  "error": {
    "name": "Error",
    "status": 500,
    "message": "Relation \"user\" is not defined for Follow model",
    "stack": "Error: Relation \"user\" is not defined for Follow model\n    at processIncludeItem ....
  }
}

我挖了一下,发现这里有一个类似的问题:https://github.com/strongloop/loopback-datasource-juggler/pull/394

但似乎我需要 loop-back-datasource-juggler >= 2.18.0 但我运行

 npm update loopback-datasource-juggler
 npm -v loopback-datasource-juggler

它告诉我:2.10.1

也许这是唯一的问题(我真的得到了最新版本吗??),或者是我的模型错了?

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    您错误地定义了关系模型。 Follow 模型与NsUser 有关系,但在NsUser 中,您定义了与user 的关系。

    请改成这样:

    "relations": {
            "followers": {
              "type": "hasMany",
              "model": "NsUser",
              "through": "Follow",
              "foreignKey": "followerUserId"
            },
            "followees": {
              "type": "hasMany",
              "model": "NsUser",
              "through": "Follow",
              "foreignKey": "followeeUserId"
            }
        }
    

    【讨论】:

    • 谢谢!太糟糕了,我没能看到这个......我最后以“followers”结束:{“type”:“hasMany”,“model”:“NsUser”,“foreignKey”:“followeeUserId”,“through”: “关注”,“keyThrough”:“followerUserId”},“followees”:{ “type”:“hasMany”,“model”:“NsUser”,“foreignKey”:“followerUserId”,“through”:“Follow”, "keyThrough": "followeeUserId" }
    【解决方案2】:

    这个问题似乎是 NPM 的问题,而不是真正的环回问题。 loopback-datasource-juggler 的 npm 页面显示最新版本是 2.49.0。

    我猜这个问题与您的(或者可能是另一个模块)package.json 文件有关,该文件描述了此依赖项。请先尝试查看您的 package.json 文件。

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 2012-07-20
      • 2013-07-01
      • 1970-01-01
      • 2011-10-13
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多