简体   繁体   English

如何在React JS中的另一个axios中使用axios响应?

[英]How to use an axios response in another axios get in React JS?

Currently i need to use an axios response in the next axios get. 目前,我需要在下一个axios获取中使用axios响应。

First get: 首先获得:

The first get returns a version ID for example 10534 . 第一个get返回一个版本ID,例如10534

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);
    });

Second get: 第二次获得:

Now i need to include the versionID in the next request 现在我需要在下一个请求中包含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) { ... }

How would i achieve this? 我将如何实现呢?

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) { ... }
}

But check your versionID it's an array and not an integer, because it's a result of map and result of map is an array. 但检查您的versionID它是一个数组,而不是一个整数,因为它的结果, map和结果map是一个数组。

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);
            });

You chain the results as regular promises. 您将结果链接为常规承诺。 You return the next axios call in the first call then get the response. 您在第一个呼叫中返回下一个axios呼叫,然后获得响应。

One way to achieve this would be to just call inside the then of your first GET and use a template string. 实现此目的的一种方法是,仅在第一个GETthen内调用并使用模板字符串。 Like so: 像这样:

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));

Alternatively you could use async/await to clean it up a bit more. 或者,您可以使用async/await清理它更多一点。 For that I'd like to refer you to this video by MPJ which explores the basic concept of async/await . 为此,我想向您推荐MPJ的这段视频,该视频探讨了async/await的基本概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM