• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

165
Vistas
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() }
)
almost 3 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

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>

almost 3 years ago · Santiago Gelvez Denunciar

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
    }))
          })
almost 3 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda