PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How to make a chain of requests with axios in js

 October 14, 2022     axios, javascript     No comments   

Issue

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

Solution

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>



Answered By - Sepehr Amirkiaee
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing