First: do not use await without a promise or a thenable object, you get only bad perfomance.
So your code would be:
getWeekOldMatches = async () => {
const dates = await getDatesRelative(-7)
var info = []
let db = await database.db()
dates.forEach(async function (date) {
var matchinfo = await database.query(db, "highlight", { "matchDetail.matchSummary.localStartDate": date })
info.push(matchinfo)
// console.log(info);;
// console.log(matchinfo);
})
// console.log(info);
return info
}
Then, the main problem here is the Array.forEach method. As you can read on MDN docs:
forEach expects a synchronous function.
forEach does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback.
So you must use a for of loop instead (much cleaner to):
getWeekOldMatches = async () => {
const dates = await getDatesRelative(-7)
var info = []
let db = await database.db()
for (const date of dates) {
var matchinfo = await database.query(db, "highlight", { "matchDetail.matchSummary.localStartDate": date })
info.push(matchinfo)
}
return info
}
The strangest thing here is that the console.log(info)
inside the forEach callback should work anyway, but i can't replicate your code.