Search code examples
sails.jssails-mongo

How to access model attributes of different controller in Sails.js?


I have two controllers/models in my Sails project which is Clubs and Members. One club can have many members.

I try to put the id of 'Clubs' as a reference id (like a foreign key) in 'Members', so that I can retrieve the members of a club by using the reference id in 'Members'. I want to display the members according to their clubs at the homepage. However I could not find a way to pass the id value of 'Clubs' to the 'Members' controller. Below are some of the codes:

Clubs.js

module.exports = {

  attributes: {

    clubName: {
      type: 'string',
    },

    clubDesc: {
      type: 'string',
    },

  },

};

Members.js

module.exports = {

  attributes: {

    memberName: {
      type: 'string',
    },

    clubId: {
      type: 'string',
    },

  },

};

ClubsController.js

module.exports = {

    list: function(req, res) {

        Clubs.find({}).exec(function(err, club) {
            if(err) {
                res.send(500, {error: 'Database Error'});
            }
            res.view('pages/club-list', {clubs:club});
        });
    },

    add: function(req, res) {
        res.view('pages/club-add');
    },

    create: function(req, res) {
        var clubName = req.body.clubName;
        var clubDesc = req.body.clubDesc;

        Clubs.create({clubName:clubName, clubDesc:clubDesc}).exec(function(err){
            if(err) {
                res.send(500, {error: 'Database Error'});
            }

            res.redirect('/clubs/list');
        });

    },

};

MembersController.js

module.exports = {

    list: function(req, res) {

        Members.find({}).exec(function(err, member) {
            if(err) {
                res.send(500, {error: 'Database Error'});
            }
            res.view('pages/member-list', {members:member});
        });
    },

    add: function(req, res) {
        res.view('pages/member-add');
    },

    create: function(req, res) {
        var memberName = req.body.memberName;
        var clubId = req.body.clubId;

        Members.create({memberName:memberName, 
        clubId:clubId}).exec(function(err){
            if(err) {
                res.send(500, {error: 'Database Error'});
            }

            res.redirect('/members/list');
        });

    },

};

routes.js

module.exports.routes = {

  '/': { 
    view: 'pages/homepage',

  },

  '/clubs/list': {
    view: 'pages/club-list',
    controller: 'Clubs',
    action: 'list'
  },

  '/clubs/add': {
    view: 'pages/club-add',
    controller: 'Clubs',
    action: 'add'
  },

  '/clubs/create': {
    controller: 'Clubs',
    action: 'create',
  },

 '/members/list': {
  view: 'pages/member-list',
  controller: 'Members',
  action: 'list'
  },

  '/members/add': {
    view: 'pages/member-add',
    controller: 'Members',
    action: 'add'
  },

  '/members/create': {
    controller: 'Members',
    action: 'create',
  },

};

I'm really new to Sails.js here and I find that it's quite difficult to get resources on this matter. I'm not sure if I put this in a way that you guys could understand. But do ask for more details if you guys need more understanding. Thank you in advance.


Solution

  • If I understand correctly, you're looking to create a one-to-many association between Clubs and Members. Here's how it should look in Clubs.js, your 'many':

    attributes: {
        ...
        members: {
            collection: 'Members',
            via: 'club'
        }
    }
    

    Then in Members.js, your 'many':

    attributes: {
         ...
         club: {
             model: 'Clubs'
         }
    }
    

    When you do Club.find(), the members key will be an array of member ids. If you do Club.find().populate('member'), the members key will be an array of fully-populated member objects.

    Here are the docs on associations.

    This isn't directly related to your question, buy since you are new to Sails, I am including a comment that will give you some advice on how to best use the framework. I hope it goes well!