-1

I need to fetch data from Mongo db using mongoose then send some request using node-fetch. So I created below async function

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);

        });


    });



}

but when I running code I am getting

**SyntaxError: await is only valid in async function**

I need to know How I run Async function insite Async function

1 Answers1

1

In this case, you're using the await inside of the function passed to forEach. You need to make that function async.

data.forEach(async (province) => ...

LazyElephant
  • 484
  • 2
  • 8