如何进行多个API调用

时间:2018-09-22 17:48:53

标签: node.js

我正在尝试对Riot API进行多个API调用,但是我在.catch(err=>)旁收到“意外令牌”语法错误。抱歉,如果我要问一个愚蠢的问题,这是我第一次使用Node.js ...

const fetch = require('node-fetch');

module.exports = (app) => {

    let champname;

    app.post('/search-champ', (req, res) => {
        champname = req.body.champname;   //added by hu
    let server = req.body.server; 
    let id= "80339518";
        //need to call api to get champions
     const apiId = 'RGAPI-da5d88a2-c56e-4b32-a640-9933a53c9058';
        const baseUrl = 'https://'+ server+'/api.riotgames.com/lol/summoner/v3/summoners/by-name/'+ champname + '?api_key='+apiId;


        const userLocation = (url1, url2, champname) => {

            let newUrl = url1 + champname + url2;
            return newUrl;
        };

        const apiUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/'+ champname + '?api_key='+apiId;

        fetch(apiUrl)
            .then(res => res.json())
            .then(data => {
                var id = data.accountId;
                console.log(data)

             const apiUrl2 = 'https://euw1.api.riotgames.com/lol/match/v3/matchlists/by-account/'+id + '?api_key='+apiId;

                fetch(apiUrl2)
                .then(res => res.json())
                .then(data => {
                    var id2=data.matches[1].gameId;
                    res.send({ data });
                    console.log('match1', data.matches[0].gameId)


                const apiUrl3='https://euw1.api.riotgames.com/lol/match/v3/matches/'+id2 + '?api_key='+apiId;

                fetch(apiUrl3)
                 .then(res => res.json())
                .then(data => {
                     res.send({ data });
                    console.log(data)

                .catch(err => {
                    res.redirect('/error');
                });
              .catch(err => {
                    res.redirect('/error');
                });
            })
            .catch(err => {
                res.redirect('/error');
            });


    })

    })
    app.get('/search-location-champ', (req, res) => {
        //build api URL with user zip
        const apiId = 'RGAPI-4b602b1a-e6aa-4c24-b88f-d0aab6467fa8';
        const baseUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/'+ champname + '?api_key='+apiId;


        const userLocation = (url1, url2, champname) => {

            let newUrl = url1 + champname + url2;
            return newUrl;
        };

        const apiUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/'+ champname + '?api_key='+apiId;


        fetch(apiUrl)
            .then(res => res.json())
            .then(data => {
                res.send({ data });
            })
            .catch(err => {
                res.redirect('/error');
            });


    })
}

4 个答案:

答案 0 :(得分:1)

检查您的代码,关闭.then回调的花括号和括号丢失或不在正确的位置,我尝试在此处进行修复。

fetch(apiUrl)
        .then(res => res.json())
        .then(data => {
            var id = data.accountId;
            console.log(data)

         const apiUrl2 = 'https://euw1.api.riotgames.com/lol/match/v3/matchlists/by- 
          account/'+id + '?api_key='+apiId;

            fetch(apiUrl2)
            .then(res => res.json())
            .then(data => {
                var id2=data.matches[1].gameId;
                res.send({ data });
                console.log('match1', data.matches[0].gameId)


            const apiUrl3='https://euw1.api.riotgames.com/lol/match/v3/matches/'+id2 
                  + '?api_key='+apiId;

            fetch(apiUrl3)
             .then(res => res.json())
            .then(data => {
                 res.send({ data });
                console.log(data)
            })
            .catch(err => {
                res.redirect('/error');
            });
           })
          .catch(err => {
                res.redirect('/error');
          });
        })
        .catch(err => {
            res.redirect('/error');
      });

答案 1 :(得分:1)

您保证可以简化链条

fetch(apiUrl)
.then(res => res.json())
.then(data => {
    return fetch(`https://euw1.api.riotgames.com/lol/match/v3/matchlists/by-account/${data.accountId}?api_key=${apiId}`);
})
.then(res => res.json())
.then(data => {
    // res.send({ data }); You cannot send response twice
    return fetch(`https://euw1.api.riotgames.com/lol/match/v3/matches/${data.matches[1].gameId}?api_key=${apiId}`)
})
.then(res => res.json())
.then(data => {
    res.send({ data }); // You can send only one response for incoming request        
})
.catch(err => {
    res.redirect('/error');
});

您只能有一个捕捞链。另外,您发送了两次响应,这可能会导致错误

答案 2 :(得分:1)

您的代码错误使用catch

我更改了:

const fetch = require('node-fetch');

module.exports = (app)=>{

    let champname;

    app.post('/search-champ', (req,res)=>{
        champname = req.body.champname;
        //added by hu
        let server = req.body.server;
        let id = "80339518";
        //need to call api to get champions
        const apiId = 'RGAPI-da5d88a2-c56e-4b32-a640-9933a53c9058';
        const baseUrl = 'https://' + server + '/api.riotgames.com/lol/summoner/v3/summoners/by-name/' + champname + '?api_key=' + apiId;

        const userLocation = (url1,url2,champname)=>{

            let newUrl = url1 + champname + url2;
            return newUrl;
        };

        const apiUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/' + champname + '?api_key=' + apiId;

        fetch(apiUrl).then(res=>res.json()).then(data=>{
            var id = data.accountId;
            console.log(data)

            const apiUrl2 = 'https://euw1.api.riotgames.com/lol/match/v3/matchlists/by-account/' + id + '?api_key=' + apiId;

            fetch(apiUrl2).then(res=>res.json()).then(data=>{
                var id2 = data.matches[1].gameId;
                res.send({
                    data
                });
                console.log('match1', data.matches[0].gameId)

                const apiUrl3 = 'https://euw1.api.riotgames.com/lol/match/v3/matches/' + id2 + '?api_key=' + apiId;

                fetch(apiUrl3).then(res=>res.json()).then(data=>{
                    res.send({
                        data
                    });
                    console.log(data)

                }).catch(err=>{
                    res.redirect('/error');
                });
            }).catch(err=>{
                res.redirect('/error');
            });

        }).catch(err=>{
            res.redirect('/error');
        });

    })
    app.get('/search-location-champ', (req,res)=>{
        //build api URL with user zip
        const apiId = 'RGAPI-4b602b1a-e6aa-4c24-b88f-d0aab6467fa8';
        const baseUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/' + champname + '?api_key=' + apiId;

        const userLocation = (url1,url2,champname)=>{

            let newUrl = url1 + champname + url2;
            return newUrl;
        };

        const apiUrl = 'https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/' + champname + '?api_key=' + apiId;

        fetch(apiUrl).then(res=>res.json()).then(data=>{
            res.send({
                data
            });
        }).catch(err=>{
            res.redirect('/error');
        });

    })
}

您可以测试它并更新您的问题(有错误)。

答案 3 :(得分:0)

当您需要来自多个来源的数据时,可以尝试使用axios all / spread。

axios
  .all([
    axios.get(https://apiurl.com/call1),
    axios.get(https://apiurl.com/call2)
  ])
  .then(
    axios.spread(
      (
        dataCall1,
        dataCall2
      ) => {
        // your code
      }
    )
  )
  .catch(function(error) {
    console.log(error);
  });

https://www.npmjs.com/package/axios