Necesito obtener datos de Mongo db usando mongoose y luego enviar una solicitud usando node-fetch. Así que creé debajo de la función asíncrona
async function mainThree() { await ProvinceList.find({}).then((data) => { data.forEach((provience) => { console.log(provience.id) let response = await fetch(`https://xxxx.com/api/getSubAddressList?addressId=${provience.id}`, { "headers": { "accept": "application/json, text/plain, */*", "accept-language": "en-US,en;q=0.9,si;q=0.8", "cache-control": "no-cache", }, "referrer": "https://www.xxxx.com/", "referrerPolicy": "strict-origin-when-cross-origin", "body": null, "method": "GET", "mode": "cors" }); let datalist = await response.json(); CityList.insertMany(datalist.module); }); }); }pero cuando ejecuto el código obtengo
**SyntaxError: await is only valid in async function**Necesito saber cómo ejecuto la función Async en el sitio Función Async
En este caso, está utilizando await dentro de la función pasada a forEach . Necesitas hacer que esa función sea asíncrona.
data.forEach(async (province) => ...