Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 异步不';mongoose升级后在回调中不返回数据_Node.js_Mongodb_Asynchronous_Mongoose_Bluebird - Fatal编程技术网

Node.js 异步不';mongoose升级后在回调中不返回数据

Node.js 异步不';mongoose升级后在回调中不返回数据,node.js,mongodb,asynchronous,mongoose,bluebird,Node.js,Mongodb,Asynchronous,Mongoose,Bluebird,在我的项目中,我使用async对数据库进行异步查询,我有以下代码: async.auto({ one: function(callback){ getFriendsIds(userId, callback) }, two: ['one', function(callback, results){ getFriendsDetails(results.one, callback); }],

在我的项目中,我使用
async
对数据库进行异步查询,我有以下代码:

async.auto({
        one: function(callback){
            getFriendsIds(userId, callback)
        },
        two: ['one', function(callback, results){
            getFriendsDetails(results.one, callback);
        }],
        final: ['one', 'two', function(callback, results) {
            res.status(200).send(results.two);
            return;
        }],
    }, function(err) {
        if (err) {
            sendError(res, err);
            return;
        }
    });
返回朋友ID的方法如下所示:

function getFriendsIds(userId, callback) {
    var query = User.findOne({_id: userId});

    query.exec(function(err, user) {
        if(err) {
            callback(err);
            return;
        }

        return callback(null, user.friendsIds);
    });
}
function getFriendsIds(userId) {
    //This will now be a bluebird promise
    //that you can return
    return User.findOne({_id: userId}).exec()
        .then(function(user){
            //return the friendIds (this is wrapped in a promise and resolved)
            return user.friendsIds;
        });
}
它工作得很好。函数返回了朋友的ID,我在异步调用的“两个”块中使用了它们

mongoose
4.3.7
升级到
4.7.8
后,它停止工作。我开始发现
Mongoose:mpromise(Mongoose的默认承诺库)已被弃用,请插入您自己的承诺库
警告,回调中不再返回ID

因此,我将
bluebird
包添加到项目中,并将其插入到
mongoose
中。现在警告消失了,但回调中仍然没有返回ID

我还将
async
升级到了最新版本,但也没用


为了实现这一点,我还需要做些什么吗?

使用承诺的想法是不使用回调,而您仍然在代码中使用回调

假设你需要像蓝知更鸟一样的

mongoose.Promise=require('bluebird')

现在,您的函数应该如下所示:

function getFriendsIds(userId, callback) {
    var query = User.findOne({_id: userId});

    query.exec(function(err, user) {
        if(err) {
            callback(err);
            return;
        }

        return callback(null, user.friendsIds);
    });
}
function getFriendsIds(userId) {
    //This will now be a bluebird promise
    //that you can return
    return User.findOne({_id: userId}).exec()
        .then(function(user){
            //return the friendIds (this is wrapped in a promise and resolved)
            return user.friendsIds;
        });
}
函数的返回值将是
user.friendsid
的数组。利用承诺的链接可能性,您可以编写一个函数来获取每个朋友的详细信息,并将其作为
friendDetails
数组返回

function getFriendsDetails(friendIds) {
    //For each friendId in friendIds, get the details from another function
    //(Promise = require("bluebird") as well)
    return Promise.map(friendIds, function(friendId) {
        //You'll have to define the getDetailsOfFriend function
        return getDetailsOfFriend(friendId);
    });
}
简单地说

getFriendsIds(123)
    .then(getFriendsDetails) //the resolved value from previous function will be passed as argument
    .then(function(friendDetails) {
        //friendDetails is an array of the details of each friend of the user with id 123
    })
    .catch(function(error){
       //Handle errors
    });

如果你想写更少的代码,你可以让bluebird推荐mongoose函数如下

Promise.promisify(User.findOne)(123)
    .then(function(user){
        return user.friendsIds;
    })
    .then(getFriendsDetails)
    .then(function(friendDetails) {
        //Return or do something with the details
    })
    .catch(function(error){
        //Handle error
    });

您是否尝试过这样的承诺,mongoose.promise=require('bluebired),mongoose.promise=global.promise,mongoose.promise=require('q')。承诺?谢谢,这几乎对我有效:)我需要做的最后一件事是向getFriendsDetails函数传递第二个参数,而不是
getFriendsDetails(ids)
应该是
getFriendsDetails(id,myId)
。我怎么做?我尝试了
.bind(null,myId)
,但它不起作用。如果要将超过先前解析的值传递给下一个
.then()
函数,只需将其编写为
。然后(function(userid){return getFriendsDetails(userid,myId);})
,假设
myId
是先前定义的。我不会更新答案,因为它与原来的问题有点太大的不同。