[英]mongodb findOne() callback arguments
The official syntax for callback is:回调的官方语法是:
user.findOne({username: "John"}).then((err,doc)=>{}
But this isnt working for me.但这对我不起作用。
Whats working for me is this:对我有用的是:
user.findOne({username: "John"}).then((doc,err)=>{}
Any suggestions why is this?任何建议为什么会这样?
A mongoose query can be executed in one of two ways.可以通过以下两种方式之一执行 mongoose 查询。 First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback.
首先,如果你传入一个回调 function,Mongoose 将异步执行查询并将结果传递给回调。
A query also has a.then() function, and thus can be used as a promise.查询也有 a.then() function,因此可以用作 promise。
1- Here pass error first 1-这里首先传递错误
Person.findOne({ 'name.last': 'Ghost' }, 'name occupation', function (err, person) {
if (err) return handleError(err);
// Prints "Space Ghost is a talk show host".
console.log('%s %s is a %s.', person.name.first, person.name.last,
person.occupation);
});
2- Here pass document first 2-这里先通过文件
Band.findOne({name: "Guns N' Roses"}).then(function(doc) { // <- this is the Promise interface.
// use doc
});
Source: Mongoose documentation资料来源: Mongoose 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.