【发布时间】:2019-09-11 18:37:43
【问题描述】:
一直在尝试从以下指南的开放 API 中获取链接/图像:https://discordjs.guide/additional-info/rest-api.html#using-node-fetch 但它不工作。我不断收到未定义的响应。
已经尝试过制作异步函数等,但没有更进一步。 还用try-catch clausule包围它来调试但找不到答案。
module.exports = {
name: 'poes',
description: 'Laat een random poes foto zien',
async execute(message, args) {
const fetch = require('node-fetch');
const {body} = await fetch('https://aws.random.cat/meow').then(response => response.json());
message.channel.send(body.file);
},
};
这就是它的用途:
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
}
);
按照指南的预期结果应该是随机的猫图像。
【问题讨论】:
-
你的json里面有body吗?
-
你是对的,它里面没有“body”。
标签: javascript api asynchronous fetch