• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

167
Views
Cómo hacer una cadena de solicitudes con axios en js

Me cuesta hacer 3 solicitudes de esta manera: necesito información de la primera para enviar las dos siguientes.

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

En this.order_main_info se almacenan el asunto y el tipo de trabajo .

Necesito enviar solicitudes:

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

y escribe los resultados en this.order_main_info así:

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

Lo he intentado muchas veces pero todavía hay algo mal con la sintaxis. Otras preguntas sobre stackoverflow no me ayudaron ((

Probé tanto async/await como

 .get () .then ( response => { axios.all() } )
almost 3 years ago · Santiago Gelvez
2 answers
Answer question

0

¿Has probado las llamadas axios anidadas?

Muestra:

 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 Report

0

Hubo algunos problemas con la construcción de la distribución de axios.

el código de trabajo final se parece a:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error