javascript - having trouble chaining multiple axios request
问题描述
Ok, so what I am trying to do is do an axios.get() request pull specific data an id specifically, then use that id that I got to put it as a string literal so I can do my second request. I keep getting Info is not defined.
axios
.get(
`https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/bloodstrive?api_key=${api}`
)
.then(response => {
info = response.data.id;
})
.then(
axios.get(
`https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/${info}?api_key=${api}`
)
)
.then(response => {
summoner = response.data;
return summoner;
});
let getSummonerId = (req, res) => {
res.status(200).send(summoner);
};
module.exports = {
getSummonerId
};
解决方案
修复你的链接:
axios
.get(
`https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/bloodstrive?api_key=${api}`
)
.then(response => {
return response.data.id;
})
.then(info => {
return axios.get(
`https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/${info}?api_key=${api}`
)
})
.then(response => {
summoner = response.data;
return summoner;
});
就个人而言,我建议将 async 用于此类任务。使用 Promise 让处理事情变得更容易:
let fetchSummoner = async() => {
const res = await axios.get(`https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/bloodstrive?api_key=${api}`);
const info = res.data.id;
const res2 = await axios.get(`https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/${info}?api_key=${api}`);
const summoner = res2.data;
return summoner;
}
推荐阅读
- python - 为什么我在 django 3.2 中没有出现反向匹配错误?
- java - 为什么 Java Message Digest 使用术语更新而不是添加?
- powershell - 我将如何运行 .exe 在一个位置运行而脚本在另一个位置运行的脚本?
- javascript - WordPress Elementor Pro 仅制作导航菜单小部件项目下拉菜单
- google-workspace - 如何允许代码从其他域运行
- git - 如何将 git clone 恢复到上次推送到 OM 时的状态?
- python - 有没有办法更快地完成这些步骤集 - 更少的步骤以及如何根据字符串值重新组织列表字符串?
- asp.net-mvc - appcmd 无法添加类型为“add”且唯一键属性“文件扩展名”设置为“.txt”的重复集合条目
- android - xamarin 连接到蓝牙 ble。应用程序可能在其主线程上做了太多工作
- amazon-web-services - AWS MWAA Cloudformation 堆栈创建失败并出现 NotStabilized