javascript - sails -mongo : many to many relationship is not working in my app. 按照文档仍然给出空数组

标签 javascript node.js mongodb sails.js sails-mongo

我正在将 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/

相关文章:

javascript - 如何处理同一数组中多个对象的 onClick

javascript - 如何停止Microsoft认知服务-语音-sdk-js以停止手动监听?

node.js - 错误: invalid_request getToken

jquery - 使用 nodejs 运行 jquery 组件

MongoDB:带参数的createCollection

node.js - JWT token 中应包含多少个人资料数据?

mongodb - 如何列出 MongoDB shell 中的所有集合?

javascript - HTML 页面被添加到 iFrame 中

javascript - 如何使用 jQuery UI 调整 datatables.js 列的大小

node.js - 使用 Node 在 THREE.js 中加载纹理