在Axios中发出多个发布请求,然后获取所有响应以发出另一个请求

时间:2020-06-25 01:43:45

标签: javascript reactjs axios directus

我已经发出了2个请求..,但是我想使用每个响应中的ID发出补丁请求...

另一个将被放入第一个,第二个将被放入数据

我们可以将其传递给var吗? idk怎么做..

我使用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)
});

1 个答案:

答案 0 :(得分:1)

我做了一个非常简化的示例,说明了您尝试使用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)
})