I have a post request being handled on my backend with express and node.js
When this post request comes in console.log(amount) is showing undefined, then 1/2 second later it reads the amount correctly.
My issue is that my route is firing off the stripe.charges.create function before the amount has time to load.
Is there a way to somehow stall this process until req.body is fully loaded?
Here is my controller function
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)
});
I am dispatching these two functions from the front end
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)
}
}
this is the second function being dispatched. These 2 functions are dispatched at the same time in the same function
// 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
});
};
this is what my console.log looks like