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

0

170
Views
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 3 years ago · Santiago Gelvez
2 answers
Answer question

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 3 years ago · Santiago Gelvez Report

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