0

I have a problem and I can't deal with it.

  1. Downloading data from the database
  2. I create loops with data from the database
  3. The records contain the key to download the photo
  4. I'm downloading pictures from getImage async
  5. if there is no photo I put: 'none'
  6. by doing res.json 'datalist' no records with a photo are returned
sqlRequest.query(sqlQuery, function(err, data, req) {
    if (err) {
        console.log(err);
        res.send(err);
    } else {

        const datalist = [];
        data.recordset.forEach(async element => {

            if (element.ImageFirst != null) {
                const get_Image = await getImage(element.KEY_BUCKET);

                const lipst_p = {
                    id: element.ID,
                    image: get_Image.Body,
                }

                datalista.push(lipst_p);


            } else {

                const lipst_p = {
                    id: element.ID,
                    image: 'none',
                }

                datalista.push(lipst_p);

            }

        });

        res.json({
            data: datalist
        });

    }
});

If I do the console.log section below :

if (element.ImageFirst != null) {
    const get_Image = await getImage(element.KEY_BUCKET);

    const lipst_p = {
        id: element.ID,
        image: get_Image.Body,
    }
    console.log(lipst_p);
    datalista.push(lipst_p);


}

gets all the correct values to console.log

lucasvw
  • 1,345
  • 17
  • 36
  • 1
    You are use an async function in a for each loop. See https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – lucasvw Nov 10 '22 at 22:00

0 Answers0