目前我需要在下一个 axios get 中使用 axios 响应。
首次获取:
第一次获取返回版本 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);
});
第二次获取:
现在我需要在下一个请求中包含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) { ... }
我如何实现这个目标?
最佳答案
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
的结果是一个数组。
关于javascript - 如何在 React JS 中的另一个 axios get 中使用 axios 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45691401/