Question

After populating data i get result not as expected.

Example:

//User.js Model
module.exports = {
  attributes: {
    //"id" attribute is here primary key by default

    schools: { collection: 'School', via: 'id' },
    name: { type: 'String', trim: true, required: true }
  }
}

And this is my School.js

//School.js Model
module.exports = {
  attributes: {
    //"id" attribute is here primary key by default

    name: { type: 'String', trim: true, required: true },
  }
}

My User entity data looks like this:

//User document in MongoDB
{
  _id: 1,
  name: "Foo",
  schools: [1,2,3]
}

My School entity data looks like this:

//School documents in MongoDB
{
    _id: 1,
    name: "School 1"
}
{
    _id: 2,
    name: "School 2"
}
{
    _id: 3,
    name: "School 3"
}

Now i want to populate the schools. I do it like this:

User.find().populate("schools").exec(function(err, res){ console.log(res[0]) });

And this is what i get as a result:

{
  schools: [Getter/Setter],
  name: "Foo",
  id: 1
}

Expected:

{
  schools: [
    {id: 1, name: "School 1"},
    {id: 2, name: "School 2"},
    {id: 3, name: "School 3"}
  ],
  name: "Foo",
  id: 1
}

How can i get expected results?

I use MongoDB as data storage.

Versions:

Sails.js: v0.10.0-rc5

Waterline: v0.10.0-rc7

sails-mongo: 0.10.0-rc2

Was it helpful?

Solution

Those are the expected results when using console.log! If you want to see the expanded object, do:

console.log(res[0].toObject());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top