3

I'm performing mysql query using nodejs/mysql.

After the first result set, I will loop through the results set and query a second table.

1) Why 'for' works and 'foreach' doesn't? What's the proper looping way to achieve what I need?

2) item = { ...item, images: rows } in the getImage function also doesn't work, why?

3) How to let the console.log show the modified results? If I use 'await', the console.log(rows) should show the modified results right?

const getImages = async (item, key) => {
    let sql = await connection.format('SELECT * from `images` WHERE id = ?', [item.id]);
    const [ rows , fields ] = await connection.execute(sql);

    item['images'] = rows
    item = { ...item, images: rows } //'<-- this method doesn't work.

    return item
}

let sql = await connection.format(`SELECT * from table ORDER BY a.listing_id LIMIT ?,?`, [0,10])

    var [rows, fields] = await connection.execute(sql);

    // rows.forEach(getImages)   //'<-- this does not work when uncommented
    // rows = rows.forEach(getImages) //'<-- this also does not work when uncommented.

    for (let i = 0; i < rows.length; i++) {    //'<-- but this works
        rows[i] = await getImages(rows[i], i);
    }

    console.log(rows) //<----- this doesn't show modified results on terminal console
    res.json(rows) //<----- browser can see the modified results on browser console
1
  • 2 years on since this question was asked, I realised that the answer below did not really answer my question. The answer to my question is simply, Foreach does not support async/await. Commented Aug 30, 2022 at 10:29

2 Answers 2

3

You have to pass to the foreach method the proper handler on each item:

let new_rows = Array()
rows.forEach(row => {
    new_rows.push(await getImages(row, i))
});

Plus, if you want to get the images on another array you should use map, its cleaner:

let new_rows = rows.map(row => {
    return await getImages(row, i)
});
Sign up to request clarification or add additional context in comments.

5 Comments

<h3> Then first, make sure the first function is working correctly: </h3> ``` ```
<h3> Then first, make sure the first function is working correctly: </h3> ``` const getImages = async (item, key) => { let sql = await connection.format('SELECT * from images WHERE id = ?', [item.id]); ... console.log(">>> Get image Return: ", item) // add this line ... } ``` <h3> Then make sure you are receiving the data: </h3> ... var [rows, fields] = await connection.execute(sql); console.log(">> Rows: ", rows) // add this line console.log(">> fields: ", fields) // add this line ... ```
my function works correctly. I will stick to for loop since there's no working .map solution
Which SQL library are you using?
I'm using mysql2 nodejs.
0

Because forEach not returning anything .So better use Array#map

enter image description here

rows = rows.map(getImages)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.