node.js - 如何让发布请求等到 req.body.value 在运行下一个函数之前加载
问题描述
我在后端使用 express 和 node.js 处理了一个发布请求
当此发布请求进入时,console.log(amount) 显示未定义,然后 1/2 秒后它会正确读取金额。
我的问题是我的路线在金额有时间加载之前触发了 stripe.charges.create 函数。
有没有办法以某种方式停止这个过程,直到 req.body 完全加载?
这是我的控制器功能
app.post('/api/stripe', async (req, res) => {
const { amount } = req.body
console.log(amount)
const charge = await stripe.charges.create({ // <-- this is firing before amount has time to load
amount: req.body.amount,
currency: 'usd',
description: 'Credit purchase',
source: req.body.id
})
console.log(req.body.amount) // < same as const { amount } = req.body
console.log(charge)
});
我从前端调度这两个函数
export const handleAmount = (amount) => async () => {
try {
const { data } = await axios({
url: "http://localhost:5000/api/stripe",
method: "POST",
data: {
amount: amount
},
withCredentials: true
}).then()
} catch (error) {
console.log(error)
}
}
这是第二个被调度的函数。这2个函数在同一个函数中同时调度
// sending stripe token to backend
export const handleToken = (token) => async dispatch => {
const res = await axios.post('http://localhost:5000/api/stripe', token);
dispatch({
type: FETCH_USER,
payload: res.data
});
};
这就是我的 console.log 的样子
解决方案
推荐阅读
- ios - 更新 cocoapods 库时 pod spec lint 或 pod lib lint 失败
- scala - 如何添加自定义指标来激发 mlib 评估器?
- android - 如何创建具有指定边框线宽度的布局?
- java - Java Stream Filter 问题将流式值与所有列表值进行比较
- shell - 在 shell 中复制查找操作的结果
- python - 为什么这个 for 循环在删除列表中的所有项目之前停止?
- sql - SQL查询查找事实表中的趋势
- javascript - 如何更改对象?
- python - 如何迭代列表列表并更改元素值?
- copy-protection - 我们应该添加到 UWF 写入过滤器例外列表中的所有 NGen 工件的列表是什么?