我正在尝试运行多个 API 请求并显示所有请求中的数据,我尝试了几种不同的方法,但它们都不包含请求中的所有数据。
我得到的最接近(这包含第二个请求中的所有内容,并且仅包含第一个请求中的“名称”:
router.get('/summoner', (req, res) => {
return axios.get('https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SUMMONERNAME', {
headers: head
})
.then(summoner => {
return axios.get('https://euw.api.riotgames.com/api/lol/EUW/v1.3/stats/by-summoner/SUMMONERID/summary?season=SEASON2017', {headers: head});
})
.then(summoner => {
res.json(summoner.data);
})
.catch(function (error) {
console.log(error);
});
});
仅包含来自第一次调用的所有数据:
router.get('/summoner2', (req, res) => {
axios.all([
axios.get('https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SUMMONERNAME', {headers: head}),
axios.get('https://euw.api.riotgames.com/api/lol/EUW/v1.3/stats/by-summoner/SUMMONERID/summary?season=SEASON2017', {headers: head})
])
.then(axios.spread(function(summonerResponse, statsResponse){
res.json(summonerResponse.data);
res.json(statsResponse.data);
}))
});
仅包含来自第一个请求的所有数据:
router.get('/summoner3', (req, res) => {
function getSummoner(){
return axios.get('https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SUMMONERNAME', {headers: head});
}
function getStats(){
return axios.get('https://euw.api.riotgames.com/api/lol/EUW/v1.3/stats/by-summoner/SUMMONERID/summary?season=SEASON2017', {headers: head});
}
axios.all([getSummoner(), getStats()])
.then(axios.spread(function (summoner, stats) {
res.json(summoner.data)
res.json(stats.data)
}));
});
我现在正在学习,所以这里可能完全错误,但任何帮助将不胜感激。
最佳答案
您只能发送一次回复。
当前您正在发送 res.json(); res.json()
两次。
它不是那样工作的。您可以在nodejs控制台中看到错误日志。
记住:一个请求,一个响应。
您只需发送一次,如下所示
res.json([summoner.data, stats.data])
关于node.js - Axios 请求 - 查看响应中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058055/