小编Xan*_*rus的帖子

返回结果mongoose查找变量查询

我需要在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)

mongoose mongodb node.js mongodb-query

23
推荐指数
2
解决办法
6万
查看次数

标签 统计

mongodb ×1

mongodb-query ×1

mongoose ×1

node.js ×1