gpt4 book ai didi

node.js - 如何进行多个API调用

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:10 26 4
gpt4 key购买 nike

我尝试对 Riot API 进行多个 API 调用,但在 .catch(err=>) 旁边收到“意外 token ”语法错误。抱歉,如果我问了一个愚蠢的问题,这是我第一次使用 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');
});


})
}

最佳答案

检查您的代码,关闭 .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');
});

关于node.js - 如何进行多个API调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52459314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com