【发布时间】:2020-12-02 18:04:34
【问题描述】:
我在将 API 的查询结果检索回变量时遇到了问题。我正在尝试使用回调函数,但它没有按预期工作。
我的问题是我从 API 得到响应,当我记录它时我可以看到它输出到控制台,但它仍然没有被设置为变量,导致变量未定义。
这是进行查询的函数及其各自的回调函数
function getResult(result)
{
let gameInfo = result;
//console.log(gameInfo); /// When uncommented, the response can be seen here, so I know my callback is getting the result.
return gameInfo;
}
function queryRAWGDatabase(callback,title)
{
title = title.split(' ').join('-');
var req = unirest("GET", "https://rawg-video-games-database.p.rapidapi.com/games/" + title);
req.headers({
"x-rapidapi-key": process.env.RAWG_GAME_DATABASE_KEY,
"x-rapidapi-host": "rawg-video-games-database.p.rapidapi.com",
"useQueryString": true
});
req.end(function (result) {
if (result.error)
{
return null;
};
return callback(result.body);
});
}
这是我调用函数并尝试将其设置为变量的地方。
async function embedBuilder(message)
{
await message.author.send("Lets get to work!\nPlease enter the title of your event. (Must be shorter than 200 characters)");
const title = await collector(message,200);
message.author.send("Please enter the name of the game. (Must be shorter than 200 characters)\nEnter \"None\" if this event does not have a game. ");
const game_title = await collector(message,200,true);
let game = queryRAWGDatabase(getResult,game_title); ////////////// CALL MADE HERE /////////
message.author.send("Please enter a short description of your event. (Must be shorter than 2000 characters)\nEnter \"None\" if no. ");
const description = await collector(message,2000,true);
console.log(game); //// When I print it here, I get undefined!
if (description != null)
{
var eventEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(title)
.setAuthor(message.author.username)
.setDescription(description)
.addField("Participants", "None")
.addField("Not Attending", "None")
.addField("Tentative", "None")
.setImage(game.background_image);
}
else // Build embed without description
{
var eventEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(title)
.setAuthor(message.author.username)
.addField("Participants", "None")
.addField("Not Attending", "None")
.addField("Tentative", "None")
.setImage(game.background_image);
}
/// . . .
最后,这是我的错误。
(node:22974) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'background_image' of undefined
at embedBuilder (/home/caleb/Programming/PlannerBot/commands/plan.js:80:24)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:22974) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22974) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
【问题讨论】:
-
什么是
game?这是一个需要“等待”的承诺吗?
标签: javascript asynchronous discord.js unirest