微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

节点:2724UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ first”

如何解决节点:2724UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ first”

错误

(node:2724) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined
    at Object.execute (C:\Users\ESP\Desktop\jetSerenity\Bots\jetMusic\Commands\upload.js:20:45)
    at processticksAndRejections (internal/process/task_queues.js:97:5)
(node:2724) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2724) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

代码

const { MessageEmbed } = require(`discord.js`);

const fs = require('fs')
const request = require(`request`);

module.exports = {
    name: 'upload',description: 'command used to upload .mp3 file',async execute(message,args){
        const HelpEmbed = new MessageEmbed()
            .setTitle('Test')
            .setColor(0xff0000)
            .setDescription('Hello there ESP')
        message.channel.send(HelpEmbed);
        // Test 
        let fileMessage = await message.channel.awaitMessages(m => m.author.id == message.author.id,{ max: 1,time: 300000 })
        request.get(fileMessage.attachments.first().url)
            .on('error',console.error)
            .pipe(fs.createWriteStream('./MusicFiles'));
    }
}

我想做什么:

我基本上希望它提示我已经设置为测试的Embed,然后等待发送附件,然后下载附件并将其放在文件夹中,我们将不胜感激,谢谢您。前进。

解决方法

解决awaitMessages后,它会为您提供消息的集合,而不仅仅是一条消息。

您已将max选项设置为1,因此它将仅包含一条消息,但该消息仍位于集合中。

尝试这样的事情:

fileMessage.first().attachments.first().url

我建议将fileMessage重命名为fileMessages,以便更清楚地知道它是一个集合。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。