Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Node.js forEach中的推送对象在贴图外观端为空_Node.js_Arrays_Object_Foreach_Push - Fatal编程技术网

Node.js forEach中的推送对象在贴图外观端为空

Node.js forEach中的推送对象在贴图外观端为空,node.js,arrays,object,foreach,push,Node.js,Arrays,Object,Foreach,Push,我希望从forEarch中推送对象,在获取forEarch中的数据后,console.log会显示正确的值,并将对象正确推送到数组中 但是在forEach的末尾,如果我尝试访问推送的对象,那么数组中就没有对象了 代码如下 async function get_line_items(items) { line_items2 = []; await items.forEach((element, index) =>

我希望从forEarch中推送对象,在获取forEarch中的数据后,console.log会显示正确的值,并将对象正确推送到数组中

但是在forEach的末尾,如果我尝试访问推送的对象,那么数组中就没有对象了

代码如下

async function get_line_items(items) {
              
            line_items2 = [];

            await items.forEach((element, index) => {

                const id = element.id;
            
                if (id) {

                    await getPriceFromCartCheckout(id, (err, results) => {
                        if (err) {
                            console.log(err);
                            return;
                        }

                        if (results && !isEmpty(results)) {
                            // Add new item
                            line_items2.push({
                                name: element.name,
                                amount: results[0].price,
                                currency: "eur",
                                quantity: element.quantity,
                            })

                        }
                        console.log(line_items2) // => Here objects are properly added to the array on each loop
                    });
                }

            })
            console.log(line_items2) // => Here the array is empty.
            return line_items2;
          }

          const line_items = await get_line_items(req.body[1].items);

          console.log( line_items); // => Here the array is empty.

任何帮助都将不胜感激:)

在此处找到解决方案:


您应该等待
getProductIdWeightPrice
,而不是循环本身。循环执行它应该执行的操作-循环数组,调用一些外部方法。这些方法做什么-循环并不关心


为了“暂停”循环,您需要等待异步调用以获取数据。这样做就会奏效:)

这不是“最后”。您不必等待
getProductIdWeight
回调。您应该让这个函数返回一个承诺,这样您就可以干净地等待它了。我刚刚更新了代码,将async/await与getPriceFromCartCheckout一起使用,但现在它告诉我:await getProductIdWeightPrice(id,(err,results)=>{^^^^^^^ SyntaxError:await仅在异步函数中有效这很好,但现在
getPriceFromCartCheckout
正在等待查询。该函数返回的基本上是db查询的结果。而该结果不是承诺(或异步函数调用)。您可以简单地执行以下操作:
getPriceFromCartCheckout:async(id)=>pool.query(`SELECT volume,price FROM products WHERE id=?`,[parseInt(id)])
如果
pool.query
返回一个承诺/异步函数,它将起作用。我将其更新为:但我仍然有相同的错误“wait仅在async函数中有效”您需要使用short return
async(id)=>pool.query
(这意味着此函数将返回对pool.query的调用),或者在函数体中显式返回它:
async(id)=>{return pool.query}
。现在执行一个函数,该函数调用pool.query,但返回void。

module.exports = {
getPriceFromCartCheckout: async (id) => {

        return await pool.query(`SELECT volume, price FROM products WHERE id = ?`, [ 
            parseInt(id)
        ])
        .then(iuidtest => {
            return iuidtest;
        });
    },
};