'Javascript await in imbricated loop error [duplicate]
I'm trying to understand to to imbricate loops in javascript and call an imported async function from an async script and I get an error. It seems like ReadinglogManager.getReadingLogForAPeriod cannot be call with await?
Main code:
var myfunction = async function(req, res, next){
students.forEach(function(s) {
dateobj.forEach(function(d) {
console.log('Date: '+d.toString())
let st= new Date();
const rl = await ReadinglogManager.getReadingLogForAPeriod(st,s.uid );
});
});
}
From ReadinglogManager:
var getReadingLogForAPeriod = async function(start_date, student_uid){
let db = DatabaseManager.fs.firestore();
let dataset = [];
let snapshot = await db.collection('ReadingLog').where('date', '>=', start_date).where('student_uid', '=', student_uid).get();
// dataset = await DatabaseManager.snapshot_to_resultset(snapshot);
await snapshot.forEach(doc => {
let tdoc=doc.data();
tdoc.uid=doc.id;
console.log("DOC: "+JSON.stringify(tdoc));
dataset.push(tdoc);
});
return Promise.resolve(dataset);
}
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|