title: nodejs query in query result loop
body: <p>I'm performing mysql query using nodejs/mysql.</p>
  
  <p>After the first result set, I will loop through the results set and query a second table.</p>
  
  <p>1) Why 'for' works and 'foreach' doesn't? What's the proper looping way to achieve what I need?</p>
  
  <p>2) item = { ...item, images: rows } in the getImage function also doesn't work, why?</p>
  
  <p>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?</p>
  
  <pre><code>const getImages = async (item, key) =&gt; {
      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 } //'&lt;-- 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)   //'&lt;-- this does not work when uncommented
      // rows = rows.forEach(getImages) //'&lt;-- this also does not work when uncommented.
  
      for (let i = 0; i &lt; rows.length; i++) {    //'&lt;-- but this works
          rows[i] = await getImages(rows[i], i);
      }
  
      console.log(rows) //&lt;----- this doesn't show modified results on terminal console
      res.json(rows) //&lt;----- browser can see the modified results on browser console
  </code></pre>