admin管理员组文章数量:1352795
Currently I am trying to run a for loop that says for each JSON object in a array create an axios post call and add it to an Array. Then I am using Axios.all to call them all at once. The issue I'm having is the JSON post object is the same for each of the post calls.
let axiosArray = []
let postData = {}
for (int x = 0; x < numOfItems; x++) {
postData['size'] = shoe.size
postData['color'] = shoe.color
let newPromise = axios({
method: 'post',
url: postShoe,
data: postData
})
.then(responsed => {
console.log('success')
})
.catch(error => {
console.log('error')
})
axiosArray.push(newPromise)
}
axios
.all(axiosArray)
.then(() => {
console.log('submitted all axios calls')
})
.catch(error => {})
It runs two post calls like I want but its using the last value supplied to postData for each of the calls. I am wondering if there is a way to save all the data in the promise as it is when I create it and then on submit it will not only use the last value supplied to the postData.
Currently I am trying to run a for loop that says for each JSON object in a array create an axios post call and add it to an Array. Then I am using Axios.all to call them all at once. The issue I'm having is the JSON post object is the same for each of the post calls.
let axiosArray = []
let postData = {}
for (int x = 0; x < numOfItems; x++) {
postData['size'] = shoe.size
postData['color'] = shoe.color
let newPromise = axios({
method: 'post',
url: postShoe,
data: postData
})
.then(responsed => {
console.log('success')
})
.catch(error => {
console.log('error')
})
axiosArray.push(newPromise)
}
axios
.all(axiosArray)
.then(() => {
console.log('submitted all axios calls')
})
.catch(error => {})
It runs two post calls like I want but its using the last value supplied to postData for each of the calls. I am wondering if there is a way to save all the data in the promise as it is when I create it and then on submit it will not only use the last value supplied to the postData.
Share Improve this question asked Mar 7, 2018 at 21:31 TotalsTotals 3112 gold badges5 silver badges14 bronze badges1 Answer
Reset to default 7axios.all
expects to receive a unhandled promise without the then
. To iterate over the responses axios.spread
accepts the responses as its arguments. The code below should work(Could certainly be more concise).
let axiosArray = []
for (int x = 0; x < numOfItems; x++) {
let postData = {}
postData['size'] = shoe.size
postData['color'] = shoe.color
let newPromise = axios({
method: 'post',
url: postShoe,
data: postData
})
axiosArray.push(newPromise)
}
axios
.all(axiosArray)
.then(axios.spread((...responses) => {
responses.forEach(res => console.log('Success'))
console.log('submitted all axios calls');
}))
.catch(error => {})
You could also refer this post which is similar to your question.
本文标签: javascriptUsing Axiosall for an array of post calls in a for loopStack Overflow
版权声明:本文标题:javascript - Using Axios.all for an array of post calls in a for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743919734a2561864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论