Javascript 不断地得到;“未定义响应”;使用使用响应对象的API时出错
我正在使用Steam Developer API创建一个Discord机器人。我使用的是ISteMuser的ResolveVanityURL调用,而且调用正确。然而,当我试图在代码中使用它时,它总是声明“响应未定义”。调用的输出非常简单,如下所示: 我的代码片段如下所示:Javascript 不断地得到;“未定义响应”;使用使用响应对象的API时出错,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,我正在使用Steam Developer API创建一个Discord机器人。我使用的是ISteMuser的ResolveVanityURL调用,而且调用正确。然而,当我试图在代码中使用它时,它总是声明“响应未定义”。调用的输出非常简单,如下所示: 我的代码片段如下所示: let{vanityURL}=superagent .得到(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&va
let{vanityURL}=superagent
.得到(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
console.log(vanityURL.response.streamid)
本质上,我使用Discord bot中的参数调用API,以从URL获取Steam ID。它调用时没有错误,但当我尝试console.log时,如上所示,它会给我以下错误:(节点:14580)未处理PromisejectionWarning:ReferenceError:未定义响应
如何才能使此错误不再继续?谢谢。您应该使用承诺来获得回复:
let{vanityURL}=superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`)
.then(res=>console.log(res));
注意在使用get之后使用then来解析承诺。您应该使用承诺来获取响应:
let{vanityURL}=superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`)
.then(res=>console.log(res));
注意在使用get之后使用then来解析承诺。首先
superagent.get()
是异步的,因此如果要直接为响应分配变量,则需要添加wait。您当前的代码试图从承诺中解构vanityURL
,这显然是未定义的
let{vanityURL}=wait superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
第二,vanityURL来自哪里?这是通话的另一个属性吗?无论哪种方式,您的steamid都位于response属性内部,因此您需要解构:
let{response,vanityURL}=wait superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
之后,您只需在响应中调用.steamid
:
console.log(response.streamid);
首先superagent.get()
是异步的,因此如果要直接为响应分配变量,则需要添加wait。您当前的代码试图从承诺中解构vanityURL
,这显然是未定义的
let{vanityURL}=wait superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
第二,vanityURL来自哪里?这是通话的另一个属性吗?无论哪种方式,您的steamid都位于response属性内部,因此您需要解构:
let{response,vanityURL}=wait superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
之后,您只需在响应中调用.steamid
:
console.log(response.streamid);
superagent.get(url)
是一个承诺,因此您需要等待它,但该调用的实际结果是什么?我看到了这张图片,但它可能意味着两件事,{steamid:“id_在这里”,success:1}
或{response:{steamid:“id_在这里”,success:1}}
那么它是哪一件呢?`第二件事,有了steamid和success的回答,这就回答了你的问题吗superagent.get(url)
是一个承诺,因此您需要等待它,但该调用的实际结果是什么?我看到了这张图片,但它可能意味着两件事,{steamid:“id_在这里”,success:1}
或{response:{steamid:“id_在这里”,success:1}}
那么它是哪一件呢?`第二件事,有了steamid和success的回答,这就回答了你的问题吗?