Anil Kumar
Anil Kumar

Reputation: 187

sails -mongo : many to many relationship is not working in my app. followed the documentation still its giving empty array

I am using sails js with mongodb.

here is my EventTags model:

EventTags.js

module.exports = {

schema: true,

  attributes: {

          eventList: {
                collection: 'Events', 
                via:'tagList'

            },  

            event_tag_name:{ type:'string',
                 required:true,
                 unique:true
            }
  }
};

here is my Events Model:

Events.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},
    }
};

created the EventTags and here is my json response.

{

    "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"
    }
}

next i copied the EventTags ID i.e."id": "55a5f725f7d707ba4f32ac74" . to Events Model. here is Events Model Post Data.

{

    "tagList":"55a5f725f7d707ba4f32ac74",

    "title": "Snow City : Bengaluru", 

    "blurb": "testtesttesttesttesttest", 

    "description": "Toronto has been chosene city",

    "venue": "palace ground" 

}

when i hit http://localhost:1337/events

I am getting empty tagList array.

[

  {

      tagList: [ ], 
      title: "Snow City ", 
      blurb: "some data",
      description: "some data", 
      venue: "some address", 
      id: "55a5f98ef7d707ba4f32ac75"
  }

] 

Please Can Anyone Help Me With This.

Upvotes: 1

Views: 559

Answers (2)

Bulkin
Bulkin

Reputation: 1040

Many-to-many relation means that a join table will be auto created by Sails. So you need to assotiate data in two tables to each other.

First of all. In models all via names are in lower case:

EventTags.js

module.exports = {

schema: false,

  attributes: {

          eventList: {
                collection: 'Events', 
                via:'taglist'

            },  

            event_tag_name:{ 
                 type:'string',
                 required:true,
                 unique:true
            }
  }
};

Events.js

module.exports = {


    schema: false,

    attributes: {

        tagList: {
                collection: 'EventTags', 
                via:'eventlist'  
            }, 

        title:{ type:'string',required: true},

        blurb:{ type:'string'},

        description:{ type:'string'},

        venue: {type: 'string',required: true},
    }
};

Next you need to associate data like:

Events.findOne({id: '55a5f98ef7d707ba4f32ac75'}).exec(function(err, event){
    if (err) {
        console.log(err);
    } else {
        event.tagList.add('55a5f725f7d707ba4f32ac74');
        event.save(function(err, saved){
            if (err) {
                console.log(err);
            } else {
                console.log(saved);
            }    
        });
    }    
});

Also you can use Blueprint to save data like:

Create data - POST /events

{
    tagList: [ '55a5f725f7d707ba4f32ac74' ], 
    title: "Snow City ", 
    blurb: "some data",
    description: "some data", 
    venue: "some address", 
}

Update data - PUT /events/55a5f98ef7d707ba4f32ac75

{
    tagList: [ '55a5f725f7d707ba4f32ac74' ], 
    title: "Snow City ", 
    blurb: "some data",
    description: "some data", 
    venue: "some address", 
}

Related documentation

Upvotes: 1

Yann Bertrand
Yann Bertrand

Reputation: 3114

In order to get the tag list of an event, you'll need to populate the tagList attribute.

Using the Blueprint API you can hit http://localhost:1337/events/55a5f98ef7d707ba4f32ac75/tagList.

In a controller, you'd use:

Event
  .findOne('55a5f98ef7d707ba4f32ac75')
  .populate('tagList')
  .exec(...);

Upvotes: 1

Related Questions