【问题标题】:Cannot read property 'split', Discord account generator bot无法读取属性“拆分”,Discord 帐户生成器机器人
【发布时间】:2021-08-29 07:50:07
【问题描述】:

无法读取属性“拆分”,我不知道如何解决此问题。

(node:21492) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of undefined
    at Client.<anonymous> (C:\Users\bradl\Desktop\account-generator-2\index.js:155:27)
    at Client.emit (events.js:376:20)
    at MessageCreateHandler.handle (C:\Users\bradl\Desktop\account-generator-2\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\bradl\Desktop\account-generator-2\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:108:65)
    at WebSocketConnection.onPacket (C:\Users\bradl\Desktop\account-generator-2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:336:35)
    at WebSocketConnection.onMessage (C:\Users\bradl\Desktop\account-generator-2\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:299:17)
    at WebSocket.onMessage (C:\Users\bradl\Desktop\account-generator-2\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:376:20)
    at Receiver.receiverOnMessage (C:\Users\bradl\Desktop\account-generator-2\node_modules\ws\lib\websocket.js:789:20)
    at Receiver.emit (events.js:376:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21492) 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:21492) [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.

在我得到错误部分的地方添加命令

if(command === "add") { 
        if (!message.member.hasPermission("ADMINISTRATOR")) return message.reply("Sorry, you can't do it, you are not an admin!");  
        let messageArray = message.content.split(" ");  
        let args = messageArray.slice(1);   
        var acc = args[1].split(":");   

【问题讨论】:

  • 向我们展示你的 index.js 大约在第 155 行。错误就在那里。你打电话给split undefined 而不是string
  • @Sysix 好了,抱歉我早该想到的
  • message.content 是可选的。你也在猜测每个内容至少有一个空格字符。
  • 尝试控制台message.content和args[1]检查它们是否是字符串
  • 记录 args 并验证是否有第二个元素

标签: javascript node.js discord.js undefined


【解决方案1】:

如果您使用的是 nodejs v14,您可以制作:

if(command === "add") { 
        if (!message.member.hasPermission("ADMINISTRATOR")) return message.reply("Sorry, you can't do it, you are not an admin!");  
        let messageArray = message.content.split(" ");  
        let args = messageArray.slice(1);   
        var acc = args[1]?.split(":");

acc 未定义但没有错误。

如果你在 nodejs v12 中,你可以做一个简单的条件:

if(command === "add") { 
        if (!message.member.hasPermission("ADMINISTRATOR")) return message.reply("Sorry, you can't do it, you are not an admin!");  
        let messageArray = message.content.split(" ");  
        let args = messageArray.slice(1);
        if(!args[1]) return;
        var acc = args[1].split(":");   

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 2021-01-24
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2020-06-03
    • 2022-01-18
    相关资源
    最近更新 更多