I want to get some data with axios, and then depending of the data that I get send a put request to the server.
All of this happends inside a express endpoint (used as a middleware between my frontend and api).
The get and put request are working as expected, but after the put request, axios seems to re-update the evaluation of "active" which results in sending res.status(200).send("Qr already used") instead of res.send("ok").
How can I prevent axios from re-evaluating the get content after put request ?
Axios is doing GET -> PUT -> GET and sending the result depending on the second GET request, where I want it to just do GET -> PUT and sending results depending on the first GET request, see the folowing output from the api:
[2021-12-26 16:00:39.499] http: GET /api/qrs?filters[uuid][$eq]=de316213 (8 ms) 200
[2021-12-26 16:00:39.531] http: PUT /api/qrs/9 (16 ms) 200
[2021-12-26 16:00:40.696] http: GET /api/qrs?filters[uuid][$eq]=de316213 (9 ms) 200
Here is my code:
app.post('/', async (req,res) => {
const uuid = req.body.uuid;
const userId = req.body.user.id;
axios.get(`http://localhost:1337/api/qrs?filters[uuid][$eq]=${uuid}`, config).then(response=>{
if(response.data.data.length >0) {
const qrId = response.data.data[0].id
const qrValue = response.data.data[0].attributes.value;
const active = response.data.data[0].attributes.active
if(active) {
res.send("ok")
consumeQr(qrId,qrValue,userId)
} else {
res.status(200).send("Qr already used")
}
} else {
res.status(400).send("QR inconnu")
}
}).catch(error => {
console.warn(error)
res.status(400).send("Error")
})
})
async function consumeQr(qrId,qrValue,userId){
disableQr(qrId).then(response =>{
return true
}).catch(error => {
return false
})
}
async function disableQr(qrId) {
axios.put(`http://localhost:1337/api/qrs/${qrId}`,
{data: {
active: false
}},
config)
}
Thank you very much, hope it's clear enough, let me know if I can add context or details :)