Loopbackjs:关注者和关注者模型(类似 Twitter)
Loopbackjs: Followers and Followees Model (Twitter-like)
我想创建一个类似 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
也许这是唯一的问题(我真的得到了最新版本吗??),或者可能是我的模型有问题?
这个问题貌似是npm的问题,并不是真正的环回问题。 loopback-datasource-juggler 的 npm 页面显示最新版本是 2.49.0。
我猜这个问题与你的(或者可能是另一个模块)package.json
描述依赖关系的文件有关。先尝试查看您的 package.json
文件。
你错误地定义了关系模型。 Follow
模型与 NsUser
相关,但在 NsUser
中,您定义了与 user
.
的关系
请改成这样:
"relations": {
"followers": {
"type": "hasMany",
"model": "NsUser",
"through": "Follow",
"foreignKey": "followerUserId"
},
"followees": {
"type": "hasMany",
"model": "NsUser",
"through": "Follow",
"foreignKey": "followeeUserId"
}
}
我想创建一个类似 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
也许这是唯一的问题(我真的得到了最新版本吗??),或者可能是我的模型有问题?
这个问题貌似是npm的问题,并不是真正的环回问题。 loopback-datasource-juggler 的 npm 页面显示最新版本是 2.49.0。
我猜这个问题与你的(或者可能是另一个模块)package.json
描述依赖关系的文件有关。先尝试查看您的 package.json
文件。
你错误地定义了关系模型。 Follow
模型与 NsUser
相关,但在 NsUser
中,您定义了与 user
.
请改成这样:
"relations": {
"followers": {
"type": "hasMany",
"model": "NsUser",
"through": "Follow",
"foreignKey": "followerUserId"
},
"followees": {
"type": "hasMany",
"model": "NsUser",
"through": "Follow",
"foreignKey": "followeeUserId"
}
}