我正在使用nodejs/mysql執行mysql查詢。在第一個結果集之后,我將遍歷結果集并查詢第二個表。1)為什么“for”有效而“foreach”不起作用?實現所需內容的正確循環方式是什么?2) 項目 = { ...item, images: rows } 在 getImage 函數中也不起作用,為什么?3)如何讓控制臺.log顯示修改后的結果?如果我使用“await”,控制臺.log(行)應該顯示修改后的結果,對嗎?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
查詢結果循環中的 nodejs 查詢
RISEBY
2022-08-04 09:50:33