Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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 如何修复异步forEach推送?_Javascript_Node.js_Asynchronous_Foreach_Sequelize.js - Fatal编程技术网

Javascript 如何修复异步forEach推送?

Javascript 如何修复异步forEach推送?,javascript,node.js,asynchronous,foreach,sequelize.js,Javascript,Node.js,Asynchronous,Foreach,Sequelize.js,当我调用自己使用node.js(postgres(Sequelize)数据库上的connection)开发的api时,它将返回以下JSON: [ { "id": 1, "name": "Wallet Name", "wallet_type": "MN", "icon": "fa fa-bank", "color": "#000000", "credit_limit": 3000, "due_day": 22 } ] 我只需要,它在

当我调用自己使用node.js(postgres(Sequelize)数据库上的connection)开发的api时,它将返回以下JSON:

[
  {
    "id": 1,
    "name": "Wallet Name",
    "wallet_type": "MN",
    "icon": "fa fa-bank",
    "color": "#000000",
    "credit_limit": 3000,
    "due_day": 22
  }
]
我只需要,它在每个对象上再返回一行(account_值),这是另一个javascript函数中的信息,所以它应该如下所示:

[
  {
    "id": 1,
    "name": "Wallet Name",
    "wallet_type": "MN",
    "icon": "fa fa-bank",
    "color": "#000000",
    "credit_limit": 3000,
    "account_value": 1200.55,
    "due_day": 22
  }
]
我目前的代码是:

异步索引(req,res){ const Wallet=等待Wallet.findAll({ 其中:{}, 属性:[ “id”, “姓名”, “钱包类型”, “图标”, “颜色”, “信用额度”, “到期日”, ], 订单:['name','ASC']], }); 常量finalObject=[]; wallet.forEach(异步钱包=>{ const currentItem=wallet.dataValues; const{id}=wait currentItem; const{sum_credits}=wait walletsreme.sumccredits(id); const{sum_debitis}=wait walletsreme.sumdebitis(id); const sum_account_value=(sum_credits-sum_debits)。toFixed(2); currentItem.account\u value=合计\u account\u value; finalObject.push(当前项); log(`pushed${id}`); }); console.log(`------------------`); console.log(finalObject); 返回res.json(finalObject); } 但是,当其返回空数组时:

[]
你能帮我吗

我不知道如何修复它(我可以更改所有代码)


非常感谢你

Array.forEach不等待异步调用。在/
for..of
或常规for循环中切换到
for..in
/
for

使用of的示例

异步索引(){
//在这里取钱包
用于(钱包的const钱包){
const currentItem=wallet.dataValues;
const{id}=wait currentItem;
const{sum_credits}=wait walletsreme.sumccredits(id);
const{sum_debitis}=wait walletsreme.sumdebitis(id);
const sum_account_value=(sum_credits-sum_debits)。toFixed(2);
currentItem.account\u value=合计\u account\u value;
finalObject.push(当前项);
log(`pushed${id}`);
}
//然后正常继续
}
在中使用的示例

异步索引(){
//在这里取钱包
用于(钱包中的const walletIndex){
const currentItem=钱包[walletIndex]。数据值;
//代码的其余部分如上所述
}
}

对不起,我只是在学习node。我试过(钱包里的钱包),但它破坏了我的代码(正如我在上所看到的)你可以告诉我,我是怎么做的吗?我的例子使用的不是Inohh,非常感谢!!!我试试看!:多姆!这是工作!我现在需要解决一个eslint问题,但是,工作得很好,ty!!!!``迭代器/生成器需要再生器运行时,这对于本指南来说太重了,不允许它们使用。另外,为了支持数组迭代,应该避免循环!将会残疾-真的,非常感谢阿比多,帮助了我很多!我在这里学到了很多;)