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

标签 node.js angular express axios

我正在尝试运行多个 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/

相关文章:

javascript - winston Elasticsearch : TypeError: Elasticsearch is not a constructor

javascript - SequelizeEagerLoadingError,无法使用包含获取数据

javascript - 从 lambda 函数 (node.js/javascript) 向 AWS Pinpoint 分段发送消息

angularjs - 你如何为 Windows 7 安装 angular-cli

node.js - 哪个 babel 插件声明了这个变量?

angular - vis-timeline 和 Angular 8 出现模块未找到错误

javascript - 如何创建新对象作为默认arg对象

Angular2 路由 : persisting route tabs and child routes

javascript - 为 JSON 属性抛出 Nan 消息

node.js - NodeJS http 获取超时