从数据库调用返回对象

时间:2019-01-06 02:39:07

标签: javascript mysql node.js asynchronous promise

我执行以下代码;

 var array = await Promise.all([
    database.query("SELECT COUNT(amount) FROM transactions WHERE date >= now() - INTERVAL 1 DAY;"), 
    database.query("SELECT COUNT(amount) FROM transactions WHERE date >= now() - INTERVAL 7 DAY;"), 
    database.query("SELECT COUNT(amount) FROM transactions"), 
  ]);

我希望像[1,2,3]这样的数组;

相反,我得到的是

[ [ RowDataPacket { 'COUNT(amount)': 6 } ],
  [ RowDataPacket { 'COUNT(amount)': 7 } ],
  [ RowDataPacket { 'COUNT(amount)': 7 } ] ]

我想念什么?

1 个答案:

答案 0 :(得分:1)

根据https://github.com/mysqljs/mysql/issues/1330,这是因为它是一个低级库。

您仍然可以使用 +-------+---------------+--------------+--------------+-------------+------------------------+----------------+ | pk_id | selling_agent | order_number | sale_price_1 | sale_price_2 | percent_of_total_order | store_location | +-------+---------------+--------------+--------------+--------------+------------------------+----------------+ | 1 | jim | 123 | 1 | 2 | 42.86 | south | | 2 | steve | 123 | 1 | 3 | 57.14 | south | | 3 | carl | 123 | 1 | 4 | 100 | north | | 4 | carl | 456 | 1 | 5 | 27.27 | north | | 5 | steve | 456 | 1 | 5 | 40.91 | north | | 6 | jim | 456 | 1 | 6 | 31.82 | north | | 7 | steve | 789 | 1 | 78 | 100 | south | | 8 | patty | 789 | 1 | 7 | 100 | north | | 9 | bob | 187 | 3 | 3 | 100 | south | +-------+---------------+--------------+--------------+----------- ---+------------------------+----------------+ 等访问该数组。如果只希望数字,那么您可能还会添加更多逻辑。