想要在节点.js中同步发送请求

Want to send request in sync in node.js

本文关键字:同步 请求 js 节点      更新时间:2023-09-26

我有一个场景,我想从集合中获取数据并插入到数组中,然后返回该数组。

router.route('/user/:_id/host/:_id/joineeList').get(function(req, res) {
    var finalList = [];
    Host.findOne({
        _id : req.params._id,
    }, function(err, host) {
        if (err) {
            res.send(err);
        } else {
            var count = host.joinees.length;
            for (var i = 0; i < count; i++) {
                console.log(host.joinees[i].userid);
                var ID = host.joinees[i].userid;
                var CODE = host.joinees[i].status_code;
                User.findOne({
                    _id : host.joinees[i].userid
                }, function(err1, user) {
                    if (err1) {
                        console.log(err1);
                    } else {
                        finalList.push({
                            "userId" : ID,
                            "name" : user.name,
                            "profilepic" : user.profilepic,
                            "status" : CODE
                        });
                        console.log(finalList);
                        // finalList[i].add = ("userId",ID);
                        // finalList[i].add = ("name",user.name);
                        // finalList[i].add = ("profilepic",user.profilepic);
                        // finalList[i].add = ("status",CODE);
                    }
                });
            }
        }
    });
});

现在,正在发生的事情是我的数组返回 null,但数据也插入到 finalList 数组中。以下是控制台输出:

[]
888888888888888888888888888888888888888888
[ { userId: '5485ae1159a751697e000003',
    name: 'aaaa',
    profilepic: 'https://graph.facebook.com/986962491123456/picture?type=large',
    status: 0 } ]
------------------------------------------
[ { userId: '5485ae1159a751697e000003',
    name: 'aaaa',
    profilepic: 'https://graph.facebook.com/123456781319929/picture?type=large',
    status: 0 },
  { userId: '5485ae1159a751697g7654003',
    name: 'ssss',
    profilepic: 'link',
    status: 0 } ]
------------------------------------------

>您可以使用此库 - https://github.com/caolan/async#eachSeries。该方法eachSeries

您的代码如下所示:

    var async = require('async');
    router.route('/user/:_id/host/:_id/joineeList').get(function (req, res) {
        var finalList = [];
        Host.findOne({
            _id: req.params._id
        }, function (err, host) {
            if (err) {
                res.send(err);
            } else {
                var count = host.joinees.length;
                var limits = [];
                for (var i = 0; i < count; i++) {
                    limits.push(i);
                }
                async.eachSeries(limits, function (i, callback) {
                    console.log(host.joinees[i].userid);
                    var ID = host.joinees[i].userid;
                    var CODE = host.joinees[i].status_code;
                    User.findOne({
                        _id: host.joinees[i].userid
                    }, function (err1, user) {
                        if (err1) {
                            console.log(err1);
                        } else {
                            finalList.push({
                                "userId": ID,
                                "name": user.name,
                                "profilepic": user.profilepic,
                                "status": CODE
                            });
                            callback();
                        }
                    });
                }, function() {
                    console.log(finalList);
                });
            }
        });
    });

让我知道它是否有帮助

您可能需要异步库的并行方法(或者可能是映射)检查出来 https://github.com/caolan/async#parallel

感谢@Vitaliy Zurian。我使用了以下代码,通过在 for 循环中使用计数器

 router.route('/user/:_id/host/:_id/joineeList').get(function (req, res) {
            var finalList = [];
            Host.findOne({
                _id: req.params._id
            }, function (err, host) {
                if (err) {
                    res.send(err);
                } else {
                    var count = host.joinees.length;
                    var limits = [];
                    for (var i = 0; i < count; i++) {
                        limits.push(i);
                    }
                async.eachSeries(limits, function (i, callback) {
                    console.log(host.joinees[i].userid);
                    var ID = host.joinees[i].userid;
                    var CODE = host.joinees[i].status_code;
                    User.findOne({
                        _id: host.joinees[i].userid
                    }, function (err1, user) {
                        if (err1) {
                            console.log(err1);
                        } else {
                            finalList.push({
                                "userId": ID,
                                "name": user.name,
                                "profilepic": user.profilepic,
                                "status": CODE
                            });
                            callback();
                        }
                    });
                }, function() {
                    console.log(finalList);
                });
            }
        });
    });