我需要在node.js中使用mongoose返回查询结果.
如何返回值以将值设置为变量?
我需要做的是:
var results = users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
if( err || !docs) {
console.log("No user found");
} else {
return docs;
};
});
Run Code Online (Sandbox Code Playgroud)
为了有:
results = docs
Run Code Online (Sandbox Code Playgroud)
非常感谢您的回复 .
我还有另一个问题.
如何使用find或findOne在查询运算符中传递变量?喜欢 :
var foo = "Blaa";
users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
if( err || !docs) {
console.log("No user found");
} else {
// I want to use the foo variable here
console.log(foo);
};
});
Run Code Online (Sandbox Code Playgroud)