1

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.

Anil Kumar
  • 187
  • 1
  • 9

2 Answers2

1

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

Bulkin
  • 1,020
  • 12
  • 27
  • Thanks for your answer. i tried your procedure and again it giving me a empty list as 'taglist'. i tried many to many relationship with category model, article model and tags, it works fine for category, articles and tags and when i included the event model, there it is not working. – Anil Kumar Jul 15 '15 at 09:57
  • yeah.. i am sure bulkin. – Anil Kumar Jul 15 '15 at 10:02
  • i followed the same procedure for my other models i.e. category, articles and tags, there its working fine. i followed the same procedure for events and tags and it is not working. i tried creating new model 'EventTags' model and tried the many to many relationship with 'Events' model, again same problem taglist[ ]. – Anil Kumar Jul 15 '15 at 10:15
  • That means, you have an error somewhere in your code. How do you associate EventTags to Events? Through Blueprint API? – Bulkin Jul 15 '15 at 10:20
  • i checked all my code, i don't have any error in my code and i corrected my faults as you shown above. and i associated the EventTags and Events as you shown above. – Anil Kumar Jul 15 '15 at 10:30
  • I updated association code with error handling and saved result debug. Try it and show what it prints to console. – Bulkin Jul 15 '15 at 10:39
  • okay.. i'll check bulkin..!! and thanks for your suggestions :) – Anil Kumar Jul 15 '15 at 10:53
1

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(...);
Yann Bertrand
  • 3,084
  • 1
  • 22
  • 38
  • i tried that too yann still it is not working. i have relationship like this -- category has N to N relationship with articles and vice versa and articles has N to N relationship with Tags and vice versa and when i included the N to N relationship with Events and Tags, there it is showing empty array of tag list. – Anil Kumar Jul 15 '15 at 09:58