简体   繁体   English

Axios请求-查看响应中的所有对象

[英]Axios Request - See All Objects in Response

I'm trying to run multiple API Requests and the display the data from all of them, I've tried it several different ways, but none of them contain ALL of the data from the requests. 我正在尝试运行多个API请求并显示所有请求的数据,我尝试了几种不同的方法,但是它们都不包含请求中的所有数据。

Closest I've got (This contains all from the 2nd Request, and only the 'Name' from the first: 我最近的(这包含来自第二个请求的所有信息,仅包含来自第一个请求的“名称”:

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

Contains all data only from the FIRST call: 仅包含FIRST调用中的所有数据:

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

Contains all data only from the FIRST request: 仅包含FIRST请求中的所有数据:

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

I'm learning at the minute, so may be completely wrong here but any help would be greatly appreciated. 我正在学习,所以这里可能是完全错误的,但是任何帮助将不胜感激。

You can only send response once. 您只能发送一次回复。 Currently you are sending res.json(); res.json() 当前您正在发送res.json(); res.json() res.json(); res.json() twice. res.json(); res.json()两次。 It's not working like that. 它不是那样工作的。 You can see error log in nodejs console. 您可以在nodejs控制台中看到错误日志。

Remember: One request, one response. 记住:一个请求,一个响应。

You have to send it only once like below 您只需要发送一次,如下所示

res.json([summoner.data, stats.data])

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

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