smileham smileham - 1 year ago 88
Node.js Question

Sail. js / Waterline.js - find() not returning array

Problem: I'm getting unexpected output from code that previously worked.

Code Problem:

sails.models.user.find().then(function (users){...});

is currently returning
{ id: 1 }

but should return an array of User objects like
[{id:x, name:y},...]

Code Alterations:

  • sails.models.user.find().exec(function (err, users){...});
    does not contain an error and returns the same as using
    like above.

  • sails.models.user.findOne(1).then(function (users){...});
    correctly returns a User like
    {id:x, name:y}

  • sails.models.venue.find().then(function (venues){...});
    returns an array of venues, just as substituting any other class besides User.


This code was previously working (it's a pretty simple line), and the only changes I made between it working and not working was running
npm install
(but it was previously working on heroku where which installed, so I don't think that was a problem) and changing the schema of User to add a few columns (I did this by deleting the User table in the DB, updating the Sails User model, and lifting the app in create mode, so the table exactly matches the model). Neither of these should cause a problem, but we all know how "should" and coding don't mix :P

How do I fix this? And why did this happen? Thanks :)

Answer Source

Realized other code was calling the package sails-mock-models which was doing its job. Totally forgot about that code. Problem solved.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download