javascript - 在 Axios 中发出多个 Post 请求,然后获取所有响应以发出另一个请求
问题描述
我已经提出了 2 个请求..,但我想使用每个响应中的 id 发出补丁请求...
另一个将放在第一个中,第二个将放在数据中
我们可以将它传递给 var 吗?我知道该怎么做..
供参考,我使用 gatsbyjs,我使用 directusCMS
let url3 = `${process.env.GATSBY_DIRECTUS_API_URL}/gemaclc/items/pendaftar/:id (the id should be from the first response that we just made)?access_token=${process.env.GATSBY_DIRECTUS_TOKEN}`;
axios.patch(url3, data, {
data: JSON.stringify(
{
featured_image: 1 (id of the second response whcih is an image),
}),
})
event.preventDefault();
const data = new FormData()
data.append('file', this.state.selectedFile)
console.warn(this.state.selectedFile);
console.log(data);
// console.log("User Email :" + this.state.email)
// console.log("User nama :" + this.state.name)
// console.log("User telepon :" + this.state.telepon)
// console.log("User program :" + JSON.stringify([this.state.program]))
// console.log("User tanggal :" + this.state.tanggal_lahir)
// console.log("User tempat :" + this.state.tempat_lahir)
let url = `${process.env.GATSBY_DIRECTUS_API_URL}/gemaclc/items/pendaftar?access_token=${process.env.GATSBY_DIRECTUS_TOKEN}`;
let url2 = `${process.env.GATSBY_DIRECTUS_API_URL}/gemaclc/files?access_token=${process.env.GATSBY_DIRECTUS_TOKEN}`;
let url2 = `${process.env.GATSBY_DIRECTUS_API_URL}/gemaclc/files?access_token=${process.env.GATSBY_DIRECTUS_TOKEN}`;
axios(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
data: JSON.stringify({
status:"published",
nama: this.state.name,
// email: this.state.email,
// telepon: this.state.telepon,
// program: [1],
// tanggal_lahir: this.state.tanggal_lahir,
// tempat_lahir: this.state.tempat_lahir,
})
})
.then(res => {
console.log(res)
return axios.post(url2, data, {
data: JSON.stringify(
{
data: data,
}),
})
})
.then(res => {
console.log(res.data.data.id)
return axios.patch( url3, {
})
})
.catch(error => {
console.log(error)
});
解决方案
我做了一个非常简化的示例,说明您尝试使用 async/await 语法来完成什么,因为 .then() 会更难阅读;基本上,您可以将每个发布请求的结果存储在一个变量中,以便在您的补丁请求中使用。我不确定您的响应对象是什么样的,因此您可能需要进行一些额外的属性提取。
//simulates 1st post request
const post1= new Promise((resolve, reject) => {
setTimeout(() => resolve(1), 1000)
});
//simulates 2nd post request
const post2= new Promise((resolve, reject) => {
setTimeout(() => resolve(2), 1000)
});
const groupedRequests = async() => {
//represents calling your 1st post request;
let id1 = await post1;
//represents calling your 2nd post request
let id2 = await post2;
//represents your patch request
console.log(id1, id2)
}
groupedRequests();
编辑:我继续做了 .then() 版本,所以你可以看到比较。
//simulates 1st post request
const post1= new Promise((resolve, reject) => {
setTimeout(() => resolve(1), 1000)
});
//simulates 2nd post request
const post2= new Promise((resolve, reject) => {
setTimeout(() => resolve(2), 1000)
});
//stores first result
let id1;
//represents callings 1st post request
post1
.then(result => {
id1 = result;
//represents calling 2nd post request
return post2;
}).then(result => {
let id2 = result;
//represents your patch request
console.log(id1, id2)
})
推荐阅读
- protractor - 量角器 - 获取 sendChromiumCommand 的结果
- php - Laravel - 添加值以选择 PDOException 时出错(代码:42000):SQLSTATE [42000]
- git - Git gui图标消失与代码
- python - 为什么调整图像大小最终也会改变通道?
- azure - 适用于小型公司的 Azure AD + 安全性
- python - 在 Flask 中获取仅返回模板/html 页面的路由
- amazon-web-services - 多次点击 API 资源时出现 Aws Api 网关 cors 问题
- python - 如何使用 KERAS 获取 DNN 最后一层的值?
- javascript - 如何添加一个会等到 firebase auth 加载的 firebase 侦听器?
- swift - 如何在 Swift 中不硬编码用户集合文档 ID 的情况下将数据添加到 Firestore Cloud