簡體   English   中英

Axios請求-查看響應中的所有對象

[英]Axios Request - See All Objects in Response

我正在嘗試運行多個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);
  });
});

僅包含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);
  }))
});

僅包含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)
  }));
});

我正在學習,所以這里可能是完全錯誤的,但是任何幫助將不勝感激。

您只能發送一次回復。 當前您正在發送res.json(); res.json() res.json(); res.json()兩次。 它不是那樣工作的。 您可以在nodejs控制台中看到錯誤日志。

記住:一個請求,一個響應。

您只需要發送一次,如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM