My code is:
if (!finalLicenses == []) {
productsOwnedString = finalLicenses.join("\n");
}
licenses.forEach(async (license) => {
const product = await Product.findOne({ _id: license.product }).exec();
console.log("New Product: " + product.name);
if (product) finalLicenses.push(product.name);
});
if (!finalLicenses == []) {
productsOwnedString = finalLicenses.join("\n");
}
print(finalLicenses)
however
if (!finalLicenses == []) {
productsOwnedString = finalLicenses.join("\n");
}
print(finalLicenses)
is running before
licenses.forEach(async (license) => {
const product = await Product.findOne({ _id: license.product }).exec();
console.log("New Product: " + product.name);
if (product) finalLicenses.push(product.name);
});
is done. I know this because My logs look like this:
[] // the finalLicenses log
New Product: test // the New Product log in the forEach
Does anyone know how I can fix this issue? I tried adding await before finalLicenses.join but that didn’t help.