[英]Strongloop Loopback: Filter by id of related Model
I have a Strongloop Loopback Node.js project with some models and relations. 我有一个Strongloop Loopback Node.js项目,包含一些模型和关系。
The problem at hand 手头的问题
My problem relates how to query only those Media
instances that have a relation to a certain Tag
id, using the Angular SDK - while not querying Tags.media
(which return Tag
instances), but instead making a query somehow that returns plain Media
instances. 我的问题涉及如何使用Angular SDK仅查询与某个Tag
id有关的Media
实例 - 而 不是查询Tags.media
(返回Tag
实例),而是以某种方式进行查询以返回普通的Media
实例。
Please read below for specific information.. 请阅读以下具体信息..
Spec 规格
Basically, I have a Model Media
which has many 'tags' (model Tag
). 基本上,我有一个模型Media
,有很多'标签'(模型Tag
)。 Think of a image file ( Media
) having various EXIF tags ( Tag
). 想象具有各种EXIF标签( Tag
)的图像文件( Media
)。 Here is the relation spec (this all works as expected): 这是关系规范(这一切都按预期工作):
Media (media.json): 媒体(media.json):
{
"name": "media",
"base": "PersistedModel",
"properties": {
"id": {
"type": "string",
"id": true
}
},
"relations": {
"tags": {
"type": "hasAndBelongsToMany",
"model": "tag"
}
}
Tag (tag.json): 标签(tag.json):
{
"name": "tag",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string",
"required": true
}
},
"relations": {
"medias": {
"type": "hasAndBelongsToMany",
"model": "media"
}
},
"acls": [],
"methods": []
}
Solutions 解决方案
Now, I know I could do a query like this (using Angular SDK in my example, but the syntax is the same): 现在,我知道我可以这样做一个查询(在我的例子中使用Angular SDK,但语法是相同的):
injector.get('Tag').find({
'filter': {
'include': 'medias',
'where': {'id': <mytagid>}
}
});
My problem with this approach is, that I receive 1 ( one
) Tag
instance with attached Media
instances. 我使用这种方法的问题是,我收到了带有附加Media
实例的1( one
) Tag
实例。 This disrupts why whole workflow as I deal only with Media
instances.. i just want to filter by Tag
id, not bother about Tag
at all. 这破坏了整个工作流程的原因,因为我只处理Media
实例..我只想按Tag
ID进行过滤 ,而不是打扰Tag
。
Bottom line 底线
If I see the API explorer ( /explorer/
), the return value of GET /api/tags/<myTagID>/medias
is exactly what I need - an array of Media
objects - but how to query them exactly like this using the Angular SDK (lb_services)? 如果我看到API资源管理器( /explorer/
), GET /api/tags/<myTagID>/medias
的返回值正是我需要的 - 一个Media
对象数组 - 但是如何使用Angular完全像这样查询它们SDK(lb_services)?
I had a similar problem. 我遇到了类似的问题。 One recommendation is to open the lb-services.js and try to find: /tags/:id/medias or something similar. 一个建议是打开lb-services.js并尝试查找:/ tags /:id / medias或类似的东西。 Then you will find a comment like this: // INTERNAL. 然后你会发现这样的评论: // INTERNAL。 Use Tags.medias() instead. 请改用Tags.medias()。 Or something similar. 或类似的东西。 So that is the method that you should call. 这就是你应该调用的方法。 Do not call the "prototype$__get....." methods. 不要调用“原型$ __ get .....”方法。
Then just call what it says there I suppose: Tag.medias({id:}) 然后只需调用它在那里所说的内容:Tag.medias({id:})
Other suggestions: 其他建议:
As you said in your description Media has many Tags. 正如您在描述中所述,媒体有很多标签。 So why not use just 那么为什么不使用呢
{
"name": "media",
"base": "PersistedModel",
"properties": {
"id": {
"type": "string",
"id": true
}
},
"relations": {
"tags": {
"type": "hasMany", <---------- hasMany
"model": "tag",
"foreignKey": "tagId" <---FK name
}
}
and for the tags just belongsTo as type. 并且对于标签而言属于类型。
{
"name": "tag",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string",
"required": true
}
},
"relations": {
"medias": {
"type": "belongsTo",
"model": "media",
"foreignKey": "mediaId" <---FK name
}
},
"acls": [],
"methods": []
}
But really I don't think this is the problem because you said when you request GET /api/tags/<myTagID>/medias
it returns what you want. 但实际上我不认为这是问题,因为你说当你请求GET /api/tags/<myTagID>/medias
它会返回你想要的东西。
Then, in AngularJS you can use: 然后,在AngularJS中,您可以使用:
Media.tags({id:<mediaId>})
for media/:id/tags 对于media /:id / tags
and for the other side try: 并为另一方尝试:
Tag.medias({id:<tagId>})
Tag.find({
filter:{
where:{mediaId: <mediaId>} <----mediaId comes from FK name
}
})
In this case both are persistent models there is no problems, I had permission problems when doing a similar thing with data that extends User type. 在这种情况下,两者都是持久性模型,没有问题,在使用扩展用户类型的数据执行类似操作时,我遇到了权限问题。 But that is another story... 不过那是另一回事了...
Hope this is helpful, I changed some stuff from a similar app that I am doing and hope not making so many errors when adapting to your code... 希望这有用,我改变了我正在做的类似应用程序的一些东西,并希望在适应您的代码时不会出现太多错误...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.