LoopBack/Node.js查询中的多个包含不同深度的对象 [英] Multiple includes on different depths in a LoopBack/Node.js query
问题描述
我正在尝试在node.js环回服务器上加载一个对象,并且想在同一查询中加载多个相关对象.这些相关对象位于不同的深度.这是查询:
I'm trying to load an object on a node.js Loopback server and I want to load multiple related objects in the same query. These related objects are on different depths. This is the query:
/api/MyUsers/114?filter[include][institutes]=institute&filter[include]=departments&filter[include]=profiles
请注意, [institutes] = institute 有两个级别,而部门和个人资料只有一个级别.
Notice that [institutes]=institute have two levels and departments and profiles only one.
这给我返回一个错误: 500内部服务器错误.错误:未为用户模型定义关系"0".
This returns me an error: 500 Internal server error. Error: Relation "0" is not defined for User model.
在node.js调试中,我注意到生成的最终JSON查询似乎是错误的:
On node.js debug I noticed that the final JSON query generated seems to be wrong:
{ include:
{ '0': 'departments',
'1': 'profiles',
institutes: 'institute' } },
...
}
这是MyUser的模型:
Here is the model for MyUser:
{
"name": "MyUser",
"plural": "MyUsers",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"mixins": {
"Versioning": true,
"Tenant": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"email": {
"type": "string"
}
}
"validations": [],
"relations": {
"departments": {
"type": "hasMany",
"model": "UserDepartment",
"foreignKey": "user_id"
},
"institutes": {
"type": "hasMany",
"model": "UserInstitute",
"foreignKey": "user_id"
},
"profiles": {
"type": "hasMany",
"model": "UserProfile",
"foreignKey": "user_id"
},
},
"acls": [],
"methods": {}
}
还有其他人遇到过这个问题吗?它是回送错误吗?如果是这样,我该如何解决?
Has anyone else faced this problem? Is it a loopback bug? If so, how can I turn around for a solution?
推荐答案
500内部服务器错误.错误:未为用户模型定义关系"0".
此错误表明您的用户模型没有称为部门的关系.在用户模型的定义中,确保已为部门模型添加了关系. (顺便说一句,如果departaments
是英语单词,则存在拼写问题,departments
是正确的.)
This error says that your User model does not have a relation which is called departaments. In your definition of the User model, make sure that you added a relation for the departaments model. (By the way, if departaments
is an English word, it has a spelling problem, departments
is correct.)
链接表示:
通过包含过滤器,您可以包含来自相关模型的结果 在查询中,例如,具有belongsTo或hasMany的模型 关系,以优化请求数.
An include filter enables you to include results from related models in a query, for example, models that have belongsTo or hasMany relations, to optimize the number of requests.
此链接可以帮助您定义模型的关系:
This link can help you to define a relation for your model:
https://strongloop. com/strongblog/定义和映射数据关系-具有环回连接模型/
如果您使用内置的用户模型,则可以按照以下链接扩展它:
If you are using the built-in User model, you can extend it as below link says:
https://loopback.io/doc/en /lb3/Extending-built-in-models.html
评论后
关于过滤器的另一种解释:
One more explanation about filters:
filter [include] [institutes] = institute过滤器的含义是:
The meaning of filter[include][institutes]=institute filter is:
包括作为用户关系的机构",还包括作为机构"的关系的机构". 我在我的一个项目中测试了此功能,效果很好.
Include 'institutes' which is a relation of User and also include 'institute' which is a relation of 'institutes'. I tested this feature in one of my projects and it works fine.
这篇关于LoopBack/Node.js查询中的多个包含不同深度的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!