类型错误:无法读取未定义的属性(读取"setTitle")。对于随机猫图片命令 - 不和谐.js V14



我正在使用Discord.js V14制作一个随机猫图片命令。我发现了一个很棒的api(https://api.thecatapi.com/v1/images/search),但我总是出错CCD_ 1。我正在使用Axios,这是我的代码:

axios.get("https://api.thecatapi.com/v1/images/search")
.then((res) => {
const embed = new EmbedBuilder()
console.log(res.data)
.setTitle("Random Cat Image")
.setImage(res.data.url)
message.reply({ embeds: [embed] });
})
.catch((err) => {
const error = String(err)
message.reply("An error occured, sorry about that. Try again in a moment. Error: " + error + ". :face_with_monocle: ")

这是数据:

[{"id":"ba5","url":"https://cdn2.thecatapi.com/images/ba5.jpg","width":500,"height":340}]

谢谢!!:(

编辑:我删除了console.log(res.data),因为它引起了问题,我不再收到错误,但是,res.data.url是未定义的。这没有意义,因为数据显然有url。

数据是一个数组,所以我需要使用res.data[0].url。感谢菲尔的帮助。

最新更新