【问题标题】:How can i fix an async forEach push? [duplicate]如何修复异步 forEach 推送? [复制]
【发布时间】:2020-03-12 14:20:34
【问题描述】:

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

[
  {
    "id": 1,
    "name": "Wallet Name",
    "wallet_type": "MN",
    "icon": "fa fa-bank",
    "color": "#000000",
    "credit_limit": 3000,
    "due_day": 22
  }
]

我只需要,它在每个对象上再返回一行(account_value),即信息在另一个 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
  }
]

我当前的代码是:

async index(req, res) {
    const wallets = await Wallet.findAll({
      where: {},
      attributes: [
        'id',
        'name',
        'wallet_type',
        'icon',
        'color',
        'credit_limit',
        'due_day',
      ],
      order: [['name', 'ASC']],
    });

    const finalObject = [];

    wallets.forEach(async wallet => {
      const currentItem = wallet.dataValues;
      const { id } = await currentItem;
      const { sum_credits } = await WalletsResume.sumCredits(id);
      const { sum_debits } = await WalletsResume.sumDebits(id);
      const sum_account_value = (sum_credits - sum_debits).toFixed(2);
      currentItem.account_value = sum_account_value;
      finalObject.push(currentItem);
      console.log(`pushed ${id}`);
    });

    console.log(`-------------------------`);
    console.log(finalObject);
    return res.json(finalObject);
  }

但是,当它返回一个空数组时:

[]

你能帮帮我吗?

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

非常感谢!

【问题讨论】:

    标签: javascript node.js asynchronous foreach sequelize.js


    【解决方案1】:

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

    使用 for-of 的示例

    async index() {
      // Fetch wallets here
    
      for (const wallet of wallets) {
        const currentItem = wallet.dataValues;
        const { id } = await currentItem;
        const { sum_credits } = await WalletsResume.sumCredits(id);
        const { sum_debits } = await WalletsResume.sumDebits(id);
        const sum_account_value = (sum_credits - sum_debits).toFixed(2);
        currentItem.account_value = sum_account_value;
        finalObject.push(currentItem);
        console.log(`pushed ${id}`);
      }
    
      // Then continue normally
    }
    

    使用 for-in 的示例

    async index() {
      // Fetch wallets here
    
      for (const walletIndex in wallets) {
        const currentItem = wallets[walletIndex].dataValues;
        // rest of code as above
      }
    
    }
    

    【讨论】:

    • 对不起,我只是学习节点。我尝试了(钱包中的钱包),但它破坏了我的密码(正如我在 developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/… 上看到的那样)你可以告诉我,我是怎么做到的?
    • 我的示例使用 for-of 而不是 for-in
    • 哦,非常感谢!!!我会尽力! :D
    • 天哪!是工作!我现在需要解决一个 eslint 问题,但是,工作得很好,ty !!!!!! ``` 迭代器/生成器需要 regenerator-runtime,这对于本指南来说太重了,不允许它们。另外,应避免循环以支持数组迭代。eslint(no-restricted-syntax) ```
    • 完成!将被禁用 - 真的,非常感谢阿比多,帮助了我很多!我在这里学到了很多;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 2020-11-30
    • 2014-07-25
    • 2018-09-17
    • 1970-01-01
    相关资源
    最近更新 更多