javascript - Node js - 返回从sequelize数据库读取的对象数组的函数

标签 javascript node.js sequelize.js

我正在尝试在 Node js 中创建一个函数,该函数读取数据库值并将它们插入数组中,然后最后返回该数组。我的功能目前如下所示:

function getInvoicesCount() {
      let promiseInvoices = [];
      let userInvCount = 0;
      let deletedUserInvCount = 0;
      let userInvAmount = 0;
      let deletedUserInvAmount = 0;
      let monthWiseInvCount = [];

      db.userInvoices
        .findAll({
          attributes: [
            'deleted_at',
            [sequelize.fn('COUNT', sequelize.col('id')), 'count'],
            [sequelize.fn('SUM', sequelize.col('invoice_amount')), 'amount'],
            [sequelize.fn('MONTH', sequelize.col('invoice_date')), 'month']
          ],
          group: ['invoice_date', 'deleted_at'],
          paranoid: false
        })
        .then(result => {
          result.forEach(function(element) {
            userInvCount += element.dataValues.count;
            userInvAmount += element.dataValues.amount;
            if (element.dataValues.deleted_at != null) {
              deletedUserInvAmount += element.dataValues.amount;
              deletedUserInvCount += element.dataValues.count;
            }
            monthWiseInvCount.push(element.dataValues);
          });
          if (monthWiseInvCount.map(a => a === 'deleted_at')) {
            monthWiseInvCount.map(a => delete a.deleted_at);
          }
          promiseInvoices.push(
            userInvCount,
            userInvAmount,
            deletedUserInvCount,
            deletedUserInvAmount,
            monthWiseInvCount
          );
        });
      return promiseInvoices;
    }

在代码的主要部分,我想调用这个函数并使用 .then 来获取返回的数组

你能帮我看看如何在函数中返回一个 promise 以及如何在 .then 部分中访问该数组吗?

最佳答案

以下是您需要进行的更改才能获得预期结果:

function getInvoicesCount() {
    ...
    return  db.userInvoices.findAll({ //<-------- 1. First add return here
        ...
    }).then(result => {
        ...
        return promiseInvoices; //<----------- 2. Put this line inside the then
    });
    // return promiseInvoices; //<----------- Remove this line from here    
}

getInvoicesCount().then(data => {
    console.log(data); // <------- Check the output here
})

Explanation for this :

To get .then when you can function , function should return promise , but in you case you are just returning a blank array ,

As per the sequlize doc , db.userInvoices.findAll returns the promise so all you need to do is add return before the this function , First step is done

You were return promiseInvoices; at wrong place , why ? , coz at that you will never get the result , as the code above it run in async manner as it is promise , so you will always get the balnk array , to get the expected result you should return it from db.userInvoices.findAll's then function as shown above in code

关于javascript - Node js - 返回从sequelize数据库读取的对象数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50597009/

相关文章:

node.js - 在 CircleCI : "npm test died unexpectedly" 上运行 Jest 时出错

node.js 扩展 foo 不是一个函数

sequelize.js - 不应该存在的属性上的非空验证错误

mysql - 无法使用 Sequelize 库连接三个具有 Sum 聚合函数的表

javascript - XMLHttpRequest - flickr - 跨源 header

javascript - 从浏览器中的对象获取数据

node.js - Mongoose 多对多关系

sequelize.js - 如何使用 sequelize 和 postgres 批量插入

javascript - 如何使用 javascript 为 google 自定义搜索添加值(value)

javascript - 无法在 chrome 上滚动容器 div 元素(移动版)