2017-08-15 18 views
0

現在、私は次のaxios getでaxios応答を使用する必要があります。React JSで別のAxiosでAxiosレスポンスを使用するには?

まずGET:

最初のgetは例のバージョンIDを返します。

axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers) 
.then(function(response) { 
    const versionID = response.data.map(function(version){  
     const vId = version.id; 

     return vId; 
    }); 

    return versionID; 
    }) 
    .catch(err => { 
     console.error(err, err.stack); 
    }); 

第二GET:

は、今私は次の要求にバージョンIDを含める必要が

axios.all([ 
    axios.get('https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = versionID ORDER BY priority DESC, key ASC', headers), 
    axios.get('https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = versionID ORDER BY priority DESC, key ASC', headers), 
    axios.get('https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = versionID ORDER BY priority DESC, key ASC', headers)) 
    ]) 
    .then(axios.spread(function (response1, response2, response3) { ... } 

私はこれをどのように実現するのでしょうか?

+0

2番目のリクエストはいつ呼び出されますか? – Andrew

+0

最初のリクエスト後 –

答えて

0
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers) 
    .then(function(response) { 
      const versionID = response.data.map(function(version) { 
       const vId = version.id; 

       return vId; 
      }); 

      return axios.all([ 
       axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
       axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
       axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers))]); 
    }).then(axios.spread(function(response1, response2, response3) { ... 
      }) 
      .catch(err => { 
       console.error(err, err.stack); 
      }); 

結果を通常の約束として連鎖します。最初の呼び出しで次のaxios呼び出しを返し、応答を取得します。

0

これを達成する方法の1つは、最初にGETthenを呼び出してテンプレート文字列を使用することです。そのよう :

const getMyStuff = new Promise((resolve, reject) => { 
    axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers) 
    .then((response) => { 
     const versionID = response.data.map(({ id }) => id); 

     axios.all([ 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
     ]) 
     .then(resolve) 
     .catch(reject) 
    }) 
    .catch(reject); 
}); 

getMyStuff() 
    .then((...args) => console.log(args)) 
    .catch((err) => console.error(err)); 

またあなたは、もう少しそれをクリーンアップするためにasync/awaitを使用することができます。 そのためには、async/awaitという基本的な概念を解説したthis video by MPJにご連絡します。

1
axios.get('https://jira.example.co.uk/rest/api/2/project/PAD/versions', headers) 
.then(function(response) { 
    const versionID = response.data.map(function(version){  
     const vId = version.id; 

     return vId; 
    }); 

    getAnotherRequest(versionID); 
    }) 
    .catch(err => { 
     console.error(err, err.stack); 
    }); 


getAnotherRequest(versionID){ 
    axios.all([ 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Reported) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers), 
     axios.get(`https://jira.example.co.uk/rest/api/2/search?jql=status IN (Confirmed) AND project = 10005 AND fixVersion = ${versionID} ORDER BY priority DESC, key ASC`, headers) 
    ]) 
    .then(axios.spread(function (response1, response2, response3) { ... } 
} 

しかし、チェックあなたのversionIDそれはmapの結果だとmapの結果が配列であるので、それは、配列ではなく整数です。

関連する問題