I want to rewrite the variable output
inside the async function(user)
. I tried after the .forEach(async function (user)
to add .then
and finally()
but the console.log(output)
stays everywhere empty.
async function findData() {
const client = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
.catch(err => { console.log(err); });
if (!client) {
return;
}
try {
var code = "";
const db = client.db(dbname);
let collection = db.collection('user');
let res = await collection.find(status: 'open').forEach(
async function (user) {
output = "User: " + user._id + " - " + await city(user.city) + output;
});
console.log(output); // the result is here ""
} catch (err) {
console.log(err);
} finally {
console.log(output); // the result is here ""
client.close();
return;
}
}
findData();
Is there a way to await the async function (user)
?