【问题标题】:Loop to get asynchronous data in nodejs sequelize在nodejs sequelize中循环获取异步数据
【发布时间】:2016-09-21 22:41:27
【问题描述】:

我正在使用 nodejs 和 sequelize 框架,但我在尝试检索一些数据时遇到问题

getAllMedicamentsWithTotal: function () {
    return medicamentService.getAll().then(function success(medicaments) {
        for(var i = 0 ; i < medicaments.length; i++){
            getTotalMedicament(medicaments[i]).then(function (total) {
                medicaments[i].dataValues.total = total;
            })
        }
    }, function fail() {

    })
}

我有这个功能,它可以获取所有药物及其库存总量。但是外部承诺在回调执行之前结束。我知道循环 promise 会出错,但有没有比修复这段代码更好的方法?

为了提供更多上下文,我有一个表库存,它具有表药物的外键,并且在库存表中有药物的数量。我想得到类似 [{name1, sum(stockQuantity1)}, {name2, sum(stockQuantity2)}, ...] 但我无法做到。

如果有任何帮助,我将不胜感激

【问题讨论】:

  • 使用 Promise.all 而不是 for 循环 bluebirdjs.com/docs/api/promise.all.html
  • 所以在外部承诺里面我会放一些类似return Promise.all(...)?的东西。我不确定在发送响应之前会执行回调函数

标签: javascript node.js promise sequelize.js


【解决方案1】:

你必须像这样把所有东西都包装在一个承诺中

getAllMedicamentsWithTotal: function () {
  return new Promise((resolve, reject) => {

    // call your service first
    medicamentService
      .getAll()

      // with your array of results we need to map out an array of promises
      // then feed that array into Promise.all() 
      .then(medicaments => Promise.all(medicaments.map(med => {
        return getTotalMedicament(med);
      }))

      // the result of Promise.all() is an array of results
      // reduce the totals to get your accumulated total and resolve it to the caller
      .then(arrayWithTotals => {
        let total = arrayWithTotals.reduce((acc, obj) => acc += obj.dataValues.total, 0);
        resolve(total);
      });
  });

// elsewhere...

getAllMedicamentsWithTotal().then(total => { /** your total */ });

顺便说一句,您似乎正在为最有可能通过join 查询完成的事情做很多逻辑。 IIRC 续集将此称为include property on your query object.

【讨论】:

  • 我发现做一个简单的 group by 可以解决问题......我仍然对 group by 和 join 有错误,但这是一个单独的问题,感谢这个答案
猜你喜欢
  • 1970-01-01
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2020-04-22
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多