您传入一个回调:
function(err, found) {
if(err)
// checks to see if there was an error
else if (found)
// checks if the document exists
}
以“立即”执行查询。这是检查文档是否存在的正确方法吗?如何判断文件是否存在?如何判断执行查询是否出错(假设在数据库返回结果之前连接已丢失)。我只是有些困惑,需要澄清一下。
您所拥有的将正常工作。
如果有错误,您将要抛出该错误。
如果查询返回了一个文档,发现将默认为true。
然后,您可以继续在第二个if语句中使用找到的对象。
要查看查询是否成功但没有找到用户:
function(err, found) {
if(err){
throw err;
}
if(found){
console.log(JSON.stringify(found));
}else{
console.log('The query was successful, but nothing was found');
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句