Francisco Possetto
Francisco Possetto

Reputation: 443

Sails Js populate don't retrieve all attributes

I have a problem with populate. I made a query to get User, Project and Topic information (Those are my 3 models). I need to show multiples dates in profile view. This is my code:

Project.js:

module.exports = {

  attributes: {

    name: {
        type: "string"
    },

    topics: {
        collection: "topic",
        via: "projects"
    },

    members: {
        collection: "user",
      via: "projects"
    },

    content: {
        collection: "content",
        via: "projectData"
    }

  }
};

Topic.js:

module.exports = {

  attributes: {

    name: {
        type: "string"
    },

    projects: {
        collection: "project",
        via: "topics"
    }

  }
};

in User.js:

show: function(req, res, next) {
   User.findOne({id: req.session.User.id}).populateAll().exec(function prjFound(err, user){
  if (err) return next(err);
  if (!user) return next();
  console.log(user);
  res.view({
      user: user
  });
});

},

Console print this:

{ projects: 
   [ { name: 'Fran',
       createdAt: '2017-06-19T21:33:17.152Z',
       updatedAt: '2017-06-19T21:33:17.190Z',
       id: 97 },
     { name: 'River Plate',
       createdAt: '2017-06-19T21:36:38.757Z',
       updatedAt: '2017-06-19T21:36:38.798Z',
       id: 98 },
     { name: 'Guido',
       createdAt: '2017-06-20T01:33:53.843Z',
       updatedAt: '2017-06-20T01:33:53.926Z',
       id: 99 } ],
  group: [],
  mat: 222222,
  name: 'Francisco',
  lastname: 'P',
  email: '[email protected]',
  encryptedPassword: '$2a$10$nKp/eAOCDPw4BS.PvQCThe42wa2/8ZABw4JzA0no9GPVT4VjFl3ZO',
  createdAt: '2017-06-19T21:32:10.535Z',
  updatedAt: '2017-06-19T21:32:10.535Z',
  id: '594842da6aeecd880ebab4e6'

}

I want to get all atributes of project model (Content, topic, and members), not only the name and id.

Anyone can explain Why my code is wrong?

Upvotes: 1

Views: 406

Answers (3)

Rahul Prajapati
Rahul Prajapati

Reputation: 11

Check this.

let result = await model.find(filter).populate("fieldName", {select:['attribute1','attribute1']})

Upvotes: 0

Sriram Jayaraman
Sriram Jayaraman

Reputation: 820

Sailsjs doesn't currently support population within a populated field. Write a query in the returned response and append it to the field that you want to populate, send the response with your desired results.

Upvotes: 1

Sangharsh
Sangharsh

Reputation: 3019

Sails/Waterline populate/populateAll do 1 level of population. For 2 or deeper level you need to write code for it.
E.g. Gather ids of user's project and do populateAll on Project.find

Upvotes: 1

Related Questions