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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.