[英]Sails.js / Waterline: findOne() vs find()
TestController.js: TestController.js:
module.exports = {
test: function(req, res) {
User.findOne({}, function(err, found) {
console.log(found);
return res.send(found);
})
}
};
The User
model contains one single entry. User
模型包含一个条目。 When this controller runs, the result that's being sent is null
or undefined
. 当此控制器运行时,正在发送的结果为
null
或undefined
。 However, if User.findOne({})
is replaced with User.find({})
, suddenly the found
variable is an array which includes the single entry: 然而,如果
User.findOne({})
被替换为User.find({})
突然found
变量是一个阵列,其包括单个条目:
[
{
"name": "Walter Jr",
"createdAt": "2014-11-16T09:59:48.232Z",
"updatedAt": "2014-11-16T09:59:48.232Z",
"id": "5468759459f51a307b47bffd"
}
]
Why? 为什么?
I didn't think this one through but for the benefit of everyone who's googling, here's what's up: 我不认为这是通过,但为了每个人的谷歌搜索的好处,这是什么了:
Once you dig into the err
, you'll find: 一旦你深入了解
err
,你会发现:
{
"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {}
}
In other words, Waterline's findOne
always requires some sort of a query to find one item; 换句话说,Waterline的
findOne
总是需要某种查询才能找到一个项目; it won't automatically find first one on any list and return it, that's not specific enough for findOne. 它不会自动在任何列表中找到第一个并返回它,这对于findOne来说不够具体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.