findOne在服务器上返回未定义
问题描述:
这是我在服务器上的代码:
Here is my code on the server:
Meteor.publish('currentRequest', function (requestId) {
console.log('from publish');
console.log(requestId) // The id is printed successfully
console.log(Requests.findOne({_id: requestId})) // returns undefined
return Requests.findOne({_id: requestId});
});
已打印项目ID,但.findOne()
似乎不起作用,因为它返回undefined
.
The item ID is printed but .findOne()
doesn't seem to work as it returns undefined
.
我在做什么错了?
答
您的问题的答案将是:因为没有满足您的搜索查询的文档.
The answer to your question will be: because there is no document satisfying your search query.
根据文档:
按照排序和跳过选项的顺序查找与选择器匹配的第一个文档.如果找不到匹配的文档,则返回
undefined
.
等同于find(selector, options).fetch()[0]
与options.limit = 1
.
此外,正如@GaëtanRouziès指出的那样,该出版物将不起作用,因为.findOne
返回document/undefined
而不是光标.
Also, as it has been pointed by @GaëtanRouziès, this publication won't work, because .findOne
returns document/undefined
instead of cursor.