![](/img/trans.png)
[英]Discord.js: TypeError: Cannot read properties of undefined (reading 'add')
[英]discord.js using axios: TypeError: Cannot read properties of undefined (reading 'ephemeral')
我正在尝试使用 Axios 为我的 Discord 机器人发出 GET 请求,但我遇到了问题。
错误:
TypeError:无法读取未定义的属性(读取“短暂”)
我使用discordjs 指南创建了这个机器人,并按照 Axios GET 请求的教程进行操作。
编码:
const { SlashCommandBuilder } = require('discord.js'); const axios = require('axios'); module.exports = { data: new SlashCommandBuilder().setName('cat2').setDescription('Random cat'), async execute(interaction) { await interaction.reply(getRandomCat()); }, }; function getRandomCat(){ axios.get('https://api.thecatapi.com/v1/images/search').then ((res) => { var data = res.data[0].url console.log('res: ', data) return data }).catch((err) => {console.error('err: ', err)}) }
该命令用于从猫 API中获取猫的随机图片。 我刚刚开始了解 discord js,所以欢迎任何帮助。 提前致谢。
尝试将getRandomCat
设为异步 function。
const { SlashCommandBuilder } = require('discord.js');
const axios = require('axios');
module.exports = {
data: new SlashCommandBuilder()
.setName('cat2')
.setDescription('Random cat'),
async execute(interaction) {
await interaction.reply(await getRandomCat());
},
};
async function getRandomCat(){
const res = await axios.get('https://api.thecatapi.com/v1/images/search').catch(e => console.error);
var data = res.data[0].url
console.log('res: ', data);
return data;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.