javascript - 并非所有在 Array.map() 调用中的 async/await 之后的元素都得到解析

标签 javascript node.js async-await

我已在此 post 中发布问题更早。在将同步功能更改为异步功能后,它终于显示了一些输出;然而,事实证明在输出数组中,一些元素为空。我不确定为什么?

主要代码如下:

router.post('/form1',  async (req, res, next)=>{

try{
    const emdate = new Date(req.body.emdate);
    const address = req.body.address;
    const stationDataCursor = stationData.filteredData(instantData, emdate);

    stationDataCursor.toArray().then(async (docArr)=>{


            return await Promise.all(docArr.map(async function(val, ind){

                try {
                    const feature = await calcDSV.calcDSV(val);                        
                    return feature

                } catch (error) {

                    console.log(ind);
                    console.log("Error happened in data array", error);

                }

            }));


    }).then((doc)=>{
        res.json(doc)
    })

} catch(error){
    res.status(400).send(error)
}
})

我尝试在 calcDSV(val) 的返回中添加 Promise.resolve(featureJSON)

async function calcDSV(featuresJSON){

    // featuresJSON  
    const SVscore = [];
    const tuEval = featuresJSON.features.properties.TU90; // array
    const obArr = featuresJSON.features.properties.OB; // array
    const periodObj = await getPeriods(tuEval);// get period position
    const paramObj = await getParams(periodObj, obArr); // get parameters
    const periodDate =  await getPeriodDate(featuresJSON, periodObj);
    const removeTime =  periodDate.beginDate.map(x=>x.split('T')[0]);


    let hourly = paramObj.hourCounts;
    let avgTemps = paramObj.avgTemps;


    for(let i = 0;i<hourly.length; i++){

        let score = await assignScore(avgTemps[i], hourly[i]);
        SVscore.push(score);

    }

    // output sv score for date


    const aggreScore = await accumScore(removeTime, SVscore);


    aggreScore.DSVdate = aggreScore.Date.map(x=>new Date(x));


    featuresJSON.features.properties.periodSV = SVscore;
    featuresJSON.features.properties.Periods = periodDate;
    featuresJSON.features.properties.DSVscore = aggreScore;

    return  Promise.resolve(featuresJSON);
}


module.exports.calcDSV = calcDSV;

错误信息:

2
Error happened in data array ReferenceError: error is not defined
at getParams (/media/swaggyp1985/HDD4T/OSU_Projects_2017-2018/App_Projects/wallin_model/dev/dev_js/wallin-server/src/utils/periodFunc.js:145:9)
at Object.calcDSV (/media/swaggyp1985/HDD4T/OSU_Projects_2017-2018/App_Projects/wallin_model/dev/dev_js/wallin-server/src/utils/wallinLogic.js:842:32)
at processTicksAndRejections (internal/process/task_queues.js:86:5)
6
Error happened in data array ReferenceError: error is not defined
at getParams (/media/swaggyp1985/HDD4T/OSU_Projects_2017-2018/App_Projects/wallin_model/dev/dev_js/wallin-server/src/utils/periodFunc.js:145:9)
at Object.calcDSV (/media/swaggyp1985/HDD4T/OSU_Projects_2017-2018/App_Projects/wallin_model/dev/dev_js/wallin-server/src/utils/wallinLogic.js:842:32)
at processTicksAndRejections (internal/process/task_queues.js:86:5)
...

我希望每个元素都能得到解决。

最佳答案

多亏了评论,我才找到了来源:我定义的其他函数中的 throw error()。应该是throw Error()

关于javascript - 并非所有在 Array.map() 调用中的 async/await 之后的元素都得到解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321250/

相关文章:

Node.js (Express) 表单在提交时清除

javascript - 在 NodeJS 中从 MongoDB 获取元素并添加到要处理的列表/数组中

javascript - 实现递归锁

node.js - Mongoose 异步/等待与 Koa 卡在等待 Model.findOne().exec()

javascript - 如何使用 JavaScript 检测 IFrame 更改 src

javascript - casperjs,可能需要另一种点击方法?

jquery - 我的导航菜单有问题。我正在使用 Bootstrap 3、JQuery 2.3.1、Jade、ExpressJS 和 Node.js

c# - 使用 async-await 可以给您带来任何性能优势吗?

javascript - HTML 如何更改单击时的图像

javascript - 在 vuex 中创建 Action 的正确方法