【问题标题】:Cannot read property 'data' of undefined discord.js无法读取未定义 discord.js 的属性“数据”
【发布时间】:2021-04-09 19:12:51
【问题描述】:

在这种情况下发出命令随机拍摄一张外福照片,并将其发布到频道中

代码在这里:

  if (command === 'waifu') {
        const waifu = new Discord.MessageEmbed()
        got('https://waifu.pics/api/sfw/waifu').then(response => {
            let content = JSON.parse(response.body);
            let waifuUrl = content[0].data.children[0].data.url;
            let waifuImage = content[0].data.children[0].data.url;
            waifu.setImage(`${waifuImage}`)
            waifu.setURL(`${waifuUrl}`)
            waifu.setColor('#ffb9f4')
            waifu.setFooter(`Requested by ${message.author.user}`)
            waifu.setTimestamp()
            waifu.setAuthor(`waifu.pics`, `https://waifu.pics/`)
            message.channel.send(waifu)
        });
    };

API 应该是正确的。经过一些小的更改后,我尝试在控制台记录 JSON,它确实输出了正确的内容。但是在不和谐的情况下运行代码时,它输出了一个 TypeError: Cannot read property 'data' of undefined 错误。我似乎无法找出问题所在

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:
     if (command === 'waifu') {
          const waifu = new Discord.MessageEmbed()
          get('https://waifu.pics/api/sfw/waifu').then(response => {
              let content = response.body;
              let waifuUrl = content.url;
              let waifuImage = content.url;
              waifu.setImage(`${waifuImage}`)
              waifu.setURL(`${waifuUrl}`)
              waifu.setColor('#ffb9f4')
              waifu.setFooter(`Requested by ${message.author.username}`)
              waifu.setTimestamp()
              waifu.setAuthor(`waifu.pics`, `https://waifu.pics/`)
              message.channel.send(waifu)
            });
        };
    

    你想的有点复杂 ^^ 你对childrendata 有所了解,尽管API 只是给出了一个URL。你甚至不必解析它。并且你已经使用了函数got(),你必须将其替换为get()

    我修复了你的代码,只是从上面复制它。

    【讨论】:

      猜你喜欢
      • 2020-10-23
      • 2022-06-10
      • 2021-06-27
      • 2020-07-20
      • 1970-01-01
      • 2020-12-05
      • 2021-04-14
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多