Can't get list relations object in relationship many-many
The case like below:
- Models : Posts, Tags
- Relations:
- Post can have many Tags
- Tags can be used in many Posts
- Models Setting:
Post:
"relations": {
"tags": {
"type": "hasMany",
"model": "Tag",
"through": "PostTag",
"foreignKey": "tagId"
}
},
PostTag
"relations": {
"post": {
"type": "belongsTo",
"model": "Post",
"foreignKey": "postId"
},
"tag": {
"type": "belongsTo",
"model": "Tag",
"foreignKey": "tagId"
}
},
Tag
"relations": {
"posts": {
"type": "hasMany",
"model": "Post",
"through": "PostTag",
"foreignKey": "postId"
}
},
You can see that i define model setting correctly. but in explorer when i make request GET "http://localhost:3000/api/Posts/5424eb28c005191b1eab726e/tags" to get all tags of a post it can't find anything (return empty array) but in mongodb i already have a record like below:
Collection: PostTag Data:
{ "_id" : { "$oid" : "5424ec57c005191b1eab726f"} , "postId" : { "$oid" : "5424eb28c005191b1eab726e"} , "tagId" : { "$oid" : "5422d501c6239308603d3811"}}
I just tried locally and everything seems to be working.
How did you create the PostTag instance? I implicitly created one with POST /api/posts/
Thanks,
Raymond Feng Co-Founder and Architect @ StrongLoop, Inc.
StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.
On Sep 25, 2014, at 10:19 PM, SangNT [email protected] wrote:
The case like below:
Models : Posts, Tags
Relations:
Post can have many Tags Tags can be used in many Posts Models Setting:
Post:
"relations": { "tags": { "type": "hasMany", "model": "Tag", "through": "PostTag", "foreignKey": "tagId" } }, PostTag
"relations": { "post": { "type": "belongsTo", "model": "Post", "foreignKey": "postId" }, "tag": { "type": "belongsTo", "model": "Tag", "foreignKey": "tagId" } }, Tag
"relations": { "posts": { "type": "hasMany", "model": "Post", "through": "PostTag", "foreignKey": "postId" } }, You can see that i define model setting correctly. but in explorer when i make request GET "http://localhost:3000/api/Posts/5424eb28c005191b1eab726e/tags" to get all tags of a post it can't find anything (return empty array) but in mongodb i already have a record like below:
Collection: PostTag Data:
{ "_id" : { "$oid" : "5424ec57c005191b1eab726f"} , "postId" : { "$oid" : "5424eb28c005191b1eab726e"} , "tagId" : { "$oid" : "5422d501c6239308603d3811"}} — Reply to this email directly or view it on GitHub.
@raymondfeng : I created PostTag by using the same way you did. but when i using debug DEBUG=loopback:connector:mongodb i saw the text below:
Fri, 26 Sep 2014 04:57:32 GMT loopback:connector:mongodb all PostTag { where: { tagId: 5424eb28c005191b1eab726e },
include: 'tag',
collect: 'tag' }
I must define like this it's work fine now: Post:
"relations": {
"tags": {
"type": "hasMany",
"model": "Tag",
"through": "PostTag",
"foreignKey": "postId"
}
},
PostTag
"relations": {
"post": {
"type": "belongsTo",
"model": "Post",
"foreignKey": "postId"
},
"tag": {
"type": "belongsTo",
"model": "Tag",
"foreignKey": "tagId"
}
},
Tag
"relations": {
"posts": {
"type": "hasMany",
"model": "Post",
"through": "PostTag",
"foreignKey": "tagId"
}
},
@raymondfeng : after i deleted and updated loopback it's ok now thanks you and thanks god.