Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
Axios is auto updating get result after put request

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 :)

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda