David Moritz
David Moritz

Reputation: 92

Populate() works in console, but not in controller

I cannot, for the life of me, figure this one out. If I run sails console and input the following code:

User.findOne(1).populate('schools').exec(console.log)
// logs user data and schools is populated`

Then I get the results that I expect. However, when I run the exact same code inside a controller, I still get all the user info, but schools is an empty array.

routes.js:

'GET /getSchools': 'UserController.getSchools'

UserController.js:

module.exports = {
  getSchools: function(req, res) {
    User.findOne(1).populate('schools').exec(console.log)
    // logs user data but schools is empty array
  }
}

Why would that be the case and how do I get it to work in the controller?

EDIT: This issue was caused because my local copy of Sails (in my node_modules folder) was version 0.11.1 while my global Sails was version 0.11.0. Reverting the Sails in my node_modules folder to be 0.11.0 fixed the issue. I hope this helps anyone else whose console environment is behaving differently than their controller environment. Thanks @sgress454!

Upvotes: 2

Views: 253

Answers (1)

David Moritz
David Moritz

Reputation: 92

This issue was caused because my local copy of Sails (in my node_modules folder) was version 0.11.1 while my global Sails was version 0.11.0. Reverting the Sails in my node_modules folder to be 0.11.0 fixed the issue. I hope this helps anyone else whose console environment is behaving differently than their controller environment. Thanks @sgress454!

Upvotes: 1

Related Questions