首页 > 解决方案 > 未捕获的类型错误:无法读取 node.js 中未定义的属性“通道”

问题描述

我需要为我的朋友创建一个垃圾邮件命令,这是我的代码:

    var msgSplit = message.content.split(" ")
    if(message.content.startsWith(prefixe+"spam"))
    {
        if(message.author.id == '389075094045196328')
        {
            var message = msgSplit[1]
            message.channel.send("1")
        }
    }

我遇到了这个问题:Uncaught TypeError: Cannot read property 'channel' of undefined 如果我输入这段代码,我不会遇到任何问题:

    if(message.content.startsWith(prefixe+"spam"))
    {
        if(message.author.id == '389075094045196328')
        {
            message.channel.send("1")
        }
    }

如果你有想法请告诉我(对不起我的英语不好:/)

标签: javascriptnode.js

解决方案


如果message.content不包含任何空格,那msgSplit[1]将是undefined因为您最终将得到msgSplit仅包含 1 个元素的数组。

const test = '111'
const splittedTest = test.split(' ')
console.log(splittedTest[0]) // 111
console.log(splittedTest[1]) // undefined

推荐阅读