我正在将 sails js 与 mongodb 一起使用。
这是我的 EventTags 模型:
事件标签.js
module.exports = {
schema: true,
attributes: {
eventList: {
collection: 'Events',
via:'tagList'
},
event_tag_name:{ type:'string',
required:true,
unique:true
}
}
};
这是我的事件模型:
事件.js
module.exports = {
schema: true,
attributes: {
tagList: {
collection: 'EventTags',
via:'eventList'
},
title:{ type:'string',required: true},
blurb:{ type:'string'},
description:{ type:'string'},
venue: {type: 'string',required: true},
}
};
创建了事件标签,这是我的 json 响应。
{
"status": 109,
"status_message": "success..!!",
"event_tag_info": {
"event_tag_name": "travel1",
"createdAt": "2015-07-15T06:01:09.050Z",
"updatedAt": "2015-07-15T06:01:09.050Z",
"id": "55a5f725f7d707ba4f32ac74"
}
}
接下来我复制了 EventTags ID,即 "id": "55a5f725f7d707ba4f32ac74"。 到事件模型。 这是事件模型发布数据。
{
"tagList":"55a5f725f7d707ba4f32ac74",
"title": "Snow City : Bengaluru",
"blurb": "testtesttesttesttesttest",
"description": "Toronto has been chosene city",
"venue": "palace ground"
}
当我点击 http://localhost:1337/events
我得到的是空的 tagList 数组。
[
{
tagList: [ ],
title: "Snow City ",
blurb: "some data",
description: "some data",
venue: "some address",
id: "55a5f98ef7d707ba4f32ac75"
}
]
谁能帮我解决这个问题。
最佳答案
为了获取事件的标签列表,您需要填充 tagList
属性。
使用 Blueprint API你可以打http://localhost:1337/events/55a5f98ef7d707ba4f32ac75/tagList .
在 Controller 中,您将使用:
Event
.findOne('55a5f98ef7d707ba4f32ac75')
.populate('tagList')
.exec(...);
关于javascript - sails -mongo : many to many relationship is not working in my app. 按照文档仍然给出空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423137/