Reputation: 885
I have posted a question in this post earlier. It finally shows some output after changing synchronous functions into asynchronous; however, it turns out that in the output array, some of the elements are null. I am not sure why?
the main code looks like this:
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)
}
})
I tried add Promise.resolve(featureJSON)
in the return of calcDSV(val)
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;
The error message:
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)
...
I expect every element would be resolved.
Upvotes: 2
Views: 75
Reputation: 885
Thanks to the comments, I was able to find out the source: the throw error()
in other functions I defined. It should be throw Error()
.
Upvotes: 1