Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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/8/magento/5.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 在map函数之后获取数据_Node.js - Fatal编程技术网

Node.js 在map函数之后获取数据

Node.js 在map函数之后获取数据,node.js,Node.js,无法获取项目。它返回[]。但它在console.log(项)上显示正确。我想在我的map()运行完成之前。它打印所有数据。如何解决这个问题。我是新来的 function getBlockUsers() { return new Promise(function (resolve, reject) { BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,

无法获取项目。它返回[]。但它在console.log(项)上显示正确。我想在我的map()运行完成之前。它打印所有数据。如何解决这个问题。我是新来的

function getBlockUsers() {
    return new Promise(function (resolve, reject) {

        BlockUser.find({userId:req.user._id}).populate("blockedId").lean().exec(function (err,result) {
            if(err){
                reject({"msg":"failed to getting block user."})
            }else{
                var results = [];
                result.map(function(item){
                    Vehicle.findOne({userId:item.blockedId}).lean().exec(function(err,vehicle){
                        if(vehicle){
                            item.vehicleId = vehicle._id;
                            item.vehicleModel = vehicle.model;
                        }
                        results.push(item)
                        console.log(item)
                    });
                });
                resolve(results);
            }


        })
    });
}

问题是result.map()中有非阻塞代码

您应该尝试只使用一个DB查询。然后解析exec回调中的所有项。否则,对原始查询使用承诺

Vehicle.find({ $in: { userId: result.map( item => item.blockedId) }}).lean().exec( (err, results) => {
    // add the vehicle / model ids to each item in results
    resolve(results)
})

由于在映射函数中使用异步函数,因此希望是同步的,因此需要创建一个promise数组,并在解析之前使用promise.all来等待所有结果

下面的代码应该可以解决您的问题

函数getBlockUsers(){ 返回新承诺(功能(解决、拒绝){ BlockUser.find({userId:req.user.\u id}).populate(“blockedId”).lean().exec(函数(err,result){ 如果(错误){ 拒绝({“msg”:“无法获取块用户。”}) }否则{ var results=result.map(函数(项){ //不要忘记返回map函数 返回新承诺(函数(resolve1、reject1){ Vehicle.findOne({userId:item.blockedId}).lean().exec(函数(err,Vehicle){ 如果(错误)返回reject1(错误) if(车辆){ item.vehicleId=车辆id; item.vehicleModel=vehicle.model; } 第1(项目) }); }) }); //在这里,你等待所有的结果承诺 解决(承诺、所有(结果)); } }) });
}map为每个结果返回一个映射项。看起来您只需要在结果项上迭代。地图中有aysnc。