gpt4 book ai didi

javascript - 返回时无法从 promise 中获取值(value)

转载 作者:行者123 更新时间:2023-12-03 01:12:32 25 4
gpt4 key购买 nike

我有一个函数可以获取特定时间段内的数据总和

  const cashData = db.query(`SELECT SUM (payments_log.amount_paid) FROM payments_log WHERE payments_log.payment_method = '2' AND payments_log.inserted_at BETWEEN $1 AND NOW() `, [date])
.then(records => {
return records[0].sum
})
.catch(error => {
cb(new Error(`Print failed: ${error}`))
})

但是当我运行它时,我的结果是[对象 promise ]。这是为什么?

最佳答案

您需要等到 promise 完成:

  const cashData = db.query(`SELECT SUM (payments_log.amount_paid) FROM payments_log WHERE payments_log.payment_method = '2' AND  payments_log.inserted_at BETWEEN $1 AND NOW() `, [date])
.then(records => {
return records[0].sum
})
.catch(error => {
cb(new Error(`Print failed: ${error}`))
});

cashData.then(res => console.log(res));

Promise Docs

关于javascript - 返回时无法从 promise 中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com