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

195
Visualizações
How to make a chain of requests with axios in js

I struggle with making 3 requests in such a way: I need an info from the first to send next two.

const order_id =  this.$route.params.order_id
          axios
          .get(`/api/v1/orders/${order_id}/`)
          .then(response => {
            this.order_main_info = response.data
          })
          .catch(err => {
            // console.log(err)
          })  

In this.order_main_info subject and worktype are stored.

I need to send requests:

first_response = axios.get(`/api/v1/subjects/${this.order_main_info.subject}/`)
second_response = axios.get(`/api/v1/worktype/${this.order_main_info.worktype}/`)

and write results into this.order_main_info like this:

this.order_main_info["worktype_name"] = first_response.data.name
this.order_main_info["subject_name"] = second_response.data.name

I've tried a lot of times but still something wrong with syntax. Other questions on stackoverflow did not help me ((

I've tried both async/await and

.get ()
.then (
response => { axios.all() }
)
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Have you tried nested axios calls?

Sample:

axios.get('https://jsonplaceholder.typicode.com/posts').then((response) => {
  const posts = response.data;
  axios.get(`https://jsonplaceholder.typicode.com/posts/${posts[0].id}`).then((response => {
    console.log(response.data)
  }))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>

about 4 years ago · Santiago Gelvez Relatório

0

There were some troubles with axios spread construction.

the final working code looks like:

      const order_id =  this.$route.params.order_id
          axios
          .get(`/api/v1/orders/${order_id}/`)
          .then(response => {
            this.order_main_info = response.data
              const subject_id = response.data.subject
            const worktype_id = response.data.work_type
            axios.all([axios.get(`/api/v1/subjects/${subject_id}`),
                       axios.get(`/api/v1/worktypes/${worktype_id}`)
            ])
            .then (
            axios.spread((firstResponse, secondResponse) => {
            this.order_main_info["subject_name"] = firstResponse.data.name
            this.order_main_info["worktype_name"]= secondResponse.data.name
    }))
          })
about 4 years ago · Santiago Gelvez Relatório
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