简体   繁体   中英

Uncaught TypeError: Cannot read property 'channel' of undefined in node.js

I need to create a spam command for my friend,there is my code:

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

And i got this problem: Uncaught TypeError: Cannot read property 'channel' of undefined if i put this code,i don't get any problem:

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

if you have an idee pls tell me (Sorry for my bad english:/)

If message.content does not contain any spaces then msgSplit[1] would be undefined because you will end up with msgSplit array only with 1 element only.

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

You are redefining message with the assignment of msgSplit[1] and so 'channel' no longer exists on that object. If you need to get the value from the index [1] and send it with the message, just reference that in your send.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM