【问题标题】:TypeError: Cannot read property 'length' of undefined - specific bugTypeError:无法读取未定义的属性“长度”-特定错误
【发布时间】:2021-02-26 12:50:54
【问题描述】:

我的机器人使用 /play 命令,该命令之前提供此列表来选择歌曲

突然给出错误“无法读取未定义的属性'长度'”,我不明白为什么它之前工作正常

这里我留下我认为发生错误的特定部分

const videos = await youtube.searchVideos(query, 5).catch(async function() {
      await message.say(
        '> There was a problem searching the video you requested >.<'
      ).then(message.react('❌'));
      return;
    });
    if (videos.length < 5 || !videos) {
      message.say(
        `> I had some trouble finding what you were looking for, please try again or be more specific`
      ).then(message.react('❌'));
      return;
    }
    const videosNameArr = [];
    for (let i = 0; i < videos.length; i++) {
      videosNameArr.push(`${i + 1}: ${videos[i].title}`);
    }
    videosNameArr.push('cancel');
    const embed = new MessageEmbed()
      .setColor('#ffffff')
      .setTitle('Choose a song/video.')
      .addField('Song 1', videosNameArr[0])
      .addField('Song 2', videosNameArr[1])
      .addField('Song 3', videosNameArr[2])
      .addField('Song 4', videosNameArr[3])
      .addField('Song 5', videosNameArr[4])
      .addField('Exit', 'Cancel');
    var songEmbed = await message.channel.send({ embed });
    message.channel
      .awaitMessages(
        function(msg) {
          return (msg.content > 0 && msg.content < 6) || msg.content === 'cancel';
        },
        {
          max: 1,
          time: 60000,
          errors: ['time']
        }
      )
      .then(function(response) {
        const videoIndex = parseInt(response.first().content);
        if (response.first().content === 'cancel') {
          songEmbed.delete().then(message.channel.send("> Song canceled"));;
          return;
        }

如果有人知道如何解决请帮助我,或者如果不向我询问命令的完整代码,我会传递它,谢谢。

【问题讨论】:

    标签: node.js discord.js ytdl


    【解决方案1】:

    好的。因此,您应该尝试使用await youtube.searchVideos(query, 5).then(videos =&gt; {,而不是使用const videos = await youtube.searchVideos(query, 5).catch(async function() {。甚至将其分成两行。


    这里你得到一个错误,因为视频没有在函数中声明,而是在外面,所以它没有内容。自己查找错误的提示是使用console.log() 检查变量及其内容,这对查找问题非常有帮助。 如果我上面的回答不能解决问题,请告诉我,我会更详细地检查。

    【讨论】:

    • 嘿,它给了我这个新错误imgur.com/4nWSGn7
    • 这很简单。你的函数不是async。确保将 async 关键字放在函数声明的前面(async function () {...} 或 async () =&gt; {...})
    • @Lioness100 我不是很明白,你能用我的代码做吗?请。我试过await youtube.searchVideos(query, 5).catch(async (videos) =&gt; {,但给了我ReferenceError: videos is not defined
    猜你喜欢
    • 2022-01-13
    • 2019-02-18
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2020-07-20
    相关资源
    最近更新 更多