Обещание .затем не дожидаясь ответа сохранить
<Если я разделю эти два запроса на почту и запустим их вручную, то все будет хорошо.
const orderHandler = () => {
setError('')
token
? axios
.post(
'http://localhost:8000/order/',
{
notes: notes,
price: quantPrice,
city: city,
street: street,
zipcode: zipcode,
},
{
headers: {
Authorization: 'Token ' + token,
},
}
)
.then((response) => {
console.log(response.data.id, "wielki chuj")
setOrderid(response.data.id )
})
//.then((response)=>setOrderid(response.data.id ))
.then(() => console.log(orderid))
.then(() => {
cartItems.map((j) =>
axios.post(
'http://localhost:8000/orderproduct/',
{
order: orderid,
product: j.id,
quantity: j.quant,
},
{
headers: {
Authorization: 'Token ' + token,
},
}
)
)
})
.catch((error) => {
console.log(error.response.data.body)
setError(error.response.data.body)
})
: setError('You must be logged in')
}
ответом было передать response.data во второй POST напрямую, а не через state. Это было предложено subparry.
.then((response) => {
cartItems.map((j) =>
axios.post(
'http://localhost:........