Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

javascript - 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 :/)

question from:https://stackoverflow.com/questions/65832600/uncaught-typeerror-cannot-read-property-channel-of-undefined-in-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...