首页 > 解决方案 > “TypeError:无法读取未定义的属性‘名称’”在 vscode

问题描述

D:\GG\PanCakeBot\index.js:18
            bot.commands.set(props.help.name, props)
                                        ^

TypeError: Cannot read property 'name' of undefined
    at D:\GG\PanCakeBot\index.js:18:41
    at Array.forEach (<anonymous>)
    at D:\GG\PanCakeBot\index.js:16:16
    at FSReqCallback.oncomplete (fs.js:171:23)

^ 当我尝试在 vscode 中使用此命令运行不和谐机器人时,这是一个错误。

fs.readdir('./commands', (err, files) => {
        if(err) return console.log(err);
        let jsfile = files.filter(f => f.split(".").pop() == 'js');

        if(jsfile.length == 0) {return console.log("Could not find any commands!")}

        jsfile.forEach(f => {
            let props = require(./commands/${f});
            bot.commands.set(props.help.name, props)
        })

我在那里错过了什么以及如何解决它?

标签: javascriptdiscord.js

解决方案


props.help未定义,因此读取props.help.name会导致错误。console.log()你,,,看看jsflile有什么问题fprops


推荐阅读