簡體   English   中英

未捕獲的類型錯誤:無法讀取 node.js 中未定義的屬性“通道”

[英]Uncaught TypeError: Cannot read property 'channel' of undefined in 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 I put this code,我沒有遇到任何問題:

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

如果你有想法請告訴我(對不起我的英語不好:/)

如果message.content不包含任何空格,則msgSplit[1]將是undefined的,因為您最終將得到僅包含 1 個元素的msgSplit數組。

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

您正在使用msgSplit[1]的分配重新定義message ,因此 object 上不再存在“通道”。 如果您需要從索引 [1] 中獲取值並將其與消息一起發送,只需在您的發送中引用它即可。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM