[英]How to make multiple API calls
I'm trying to make multiple API calls to the Riot API, but I am getting an 'Unexpected token' syntax error next to .catch(err=>)
. 我正在尝试对Riot API进行多个API调用,但是在.catch(err=>)
旁边出现“意外令牌”语法错误。 Sorry if I'm asking a stupid question, it's my first time using Node.js... 抱歉,如果我要问一个愚蠢的问题,这是我第一次使用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');
});
})
}
Check your code the curly braces and parenthesis that closes the .then callback are missing or not in right place, I tried to fix it here. 检查您的代码,关闭.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');
});
You promise chains can be simplified 您保证可以简化链条
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');
});
You can have only one catch chain. 您只能有一个捕捞链。 Also you sent response twice it could cause an error 您也发送了两次响应,这可能会导致错误
Your code wrong use catch
, 您的代码使用错误catch
,
I change it: 我改变它:
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');
});
})
}
You can test it and update your question (with error). 您可以测试它并更新您的问题(有错误)。
You can try using axios all / spread when you need data from multiple origins. 当您需要来自多个来源的数据时,可以尝试使用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 https://www.npmjs.com/package/axios
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.