【发布时间】:2021-01-11 01:41:03
【问题描述】:
我想提取一个返回用户购物车总值的变量,但说该变量不存在。
Cart.findAll({where: {userId: 1}}).then((cart) =>{
if (cart != undefined) {
let price = []
let quantity = []
cart.forEach((item) => {
price.push(item.price * item.quantity)
})
const total = Number(price.reduce((a, b) => (a + b), 0))
return total
}
})
MercadoPago.configure({
sandbox: true,
access_token: ''
})
router.get('/pay', islogged, async (req, res) => {
let id = '' + Date.now()
let data = {
items: [
item = {
id: id,
title: "",
description: '',
quantity: 1,
currency_id: "BRL",
unit_price: parseFloat(total) // here <<<<
}
],
payer: {
email: req.session.user['email']
},
auto_return : "all",
external_reference: id,
back_urls: {
success: getFullUrl(req) + '/success',
failure: getFullUrl(req) + '/failure',
pending: getFullUrl(req) + '/pending'
}
}
try {
let payment = await MercadoPago.preferences.create(data)
return res.redirect(payment.body.init_point)
} catch(err) {
return res.send(err.message)
}
})
我想得到这个total 变量。更好地解释,我需要访问支付路径中的总变量,以添加产品的总价值。但是,由于它在 Promise 中,我无法访问此变量,这是错误
【问题讨论】:
-
你能说出确切的错误吗?
-
@HemantJain 总数未定义
-
试图拉取函数,它返回未定义
-
如何检查函数的结果?
-
请显示完整的代码。你在哪里使用
total?您究竟在哪一行得到错误?
标签: node.js promise sequelize.js