Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查询结果循环中的nodejs查询_Javascript_Mysql_Node.js - Fatal编程技术网

Javascript 查询结果循环中的nodejs查询

Javascript 查询结果循环中的nodejs查询,javascript,mysql,node.js,Javascript,Mysql,Node.js,我正在使用nodejs/mysql执行mysql查询 在第一个结果集之后,我将遍历结果集并查询第二个表 1) 为什么“for”有效而“foreach”无效?什么是正确的循环方式来实现我所需要的 2) getImage函数中的item={…item,images:rows}也不起作用,为什么 3) 如何让console.log显示修改后的结果?如果我使用'await',console.log(行)应该显示修改的结果,对吗 const getImages = async (item, key) =&

我正在使用nodejs/mysql执行mysql查询

在第一个结果集之后,我将遍历结果集并查询第二个表

1) 为什么“for”有效而“foreach”无效?什么是正确的循环方式来实现我所需要的

2) getImage函数中的item={…item,images:rows}也不起作用,为什么

3) 如何让console.log显示修改后的结果?如果我使用'await',console.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
const getImages=async(项,键)=>{
让sql=wait connection.format('SELECT*from'images`WHERE-id=?',[item.id]);
const[rows,fields]=等待连接。执行(sql);
项['images']=行
item={…item,images:rows}/'因为不返回任何内容。所以最好使用


必须向foreach方法传递每个项的适当处理程序:

let new_rows = Array()
rows.forEach(row => {
    new_rows.push(await getImages(row, i))
});
另外,如果要在另一个阵列上获取图像,则应使用map及其清洁器:

let new_rows = rows.map(row => {
    return await getImages(row, i)
});

然后,首先确保第一个函数正常工作:```````然后,确保第一个函数正常工作:```const getImages=async(item,key)=>{let sql=wait connection.format('SELECT*from
images
WHERE id=?',[item.id]);…console.log(“>>Get image Return:”,item)//添加此行…}```然后确保您正在接收数据:…var[行,字段]=等待连接。执行(sql);console.log(“>>行:”,行)//添加此行console.log(“>>字段:”,字段)//添加这一行…``我的函数工作正常。我将坚持使用for循环,因为没有工作。映射解决方案您使用的是哪个SQL库?我使用的是mysql2 nodejs。
let new_rows = rows.map(row => {
    return await getImages(row, i)
});