![](/img/trans.png)
[英]Make Multiple Post Requests In Axios then get all the response to make another request
[英]Make Multiple Post Requests In Axios
我一直想做的事情是為我的博客文章打一個端點,然后使用此數據刪除Wordpress帶來的額外布局標記。 我使用Axios發出請求,然后轉換響應選項,以便修改數據以從響應中的“ post_body”對象中刪除多余的標記。 這對單個博客文章有效,但是當我嘗試執行所有我的博客博客文章時,它將返回20個左右博客文章的對象。 我要做的是遍歷對象,轉換數據,然后向另一個API發出發布請求,以發布我的博客文章。 一旦我的諾言得到解決,我不知道這是否有可能。 我能否在.then中創建另一個for循環並找到我的“ post_body”對象並發出發布請求。 不知道我是否在考慮正確的方法。 任何幫助深表感謝。
var fieldName = "et_pb";
var regExp = new RegExp("\\[\/?(" + fieldName + ".*?)\\]", "g");
function getBlogPosts() {
return axios.get(allPosts, {
transformResponse: axios.defaults.transformResponse.concat(function(data, headers) {
// use data I passed into the function and the objects from the API
// pass in data into the function using forEach this will return an array
data.objects.forEach(function(i) {
// use the returned array on Objects.key to find the name of the array
Object.keys(i).forEach(function(k) {
// if the key equals execute code
// console.log(k);
if (k === "post_body") {
// fire Regex
data[k] = i[k].replace(regExp, '');
// console.log(data[k])
}
})
})
return data;
})
})
}
axios.all([getBlogPosts()])
.then(axios.spread(function(blogResponse) {
console.log(blogResponse.data);
}));
@詹姆斯,你是正確的。 您可以按以下方式鏈接多個請求,也可以使用asyn和await選項。
axios.get(...) // for allPosts
.then((response) => {
return axios.get(...); // using response.data
})
.then((response) => {
console.log('Response', response);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.