Sails.js / Waterline: findOne () vs find ()
TestController.js:
module.exports = {
test: function(req, res) {
User.findOne({}, function(err, found) {
console.log(found);
return res.send(found);
})
}
};
The model User
contains one single entry. When this controller is running, the send result will be null
or undefined
. However, if User.findOne({})
replaced with User.find({})
, all of a sudden the variable found
is an array that includes one entry:
[
{
"name": "Walter Jr",
"createdAt": "2014-11-16T09:59:48.232Z",
"updatedAt": "2014-11-16T09:59:48.232Z",
"id": "5468759459f51a307b47bffd"
}
]
Why?
source to share
I didn't think about it, but for the good of everyone who is adventurous in the game, here's what:
Once you dig into err
, you will find:
{
"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {}
}
In other words, Waterline findOne
always requires some kind of query to find a single item; it won't automatically find the first one in any list and return it, which is not enough for findOne.
source to share