提问者:小点点

Discordjs:console log=menght不是不受保护的


嗨! 在控制台。log上说“长度未定义”。。。我不明白为什么。 使用以下代码:(问题=lign:25)

const Discord = require("discord.js")
const config = require("./config.json")
const bot = new Discord.Client();
const fs = require("fs");
bot.commands = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {

  if(err) console.log(err);

  let jsfile = files.filter(f => f.split(".").pop() === "js");
  if(jsfile.length <= 0){
    console.log("Je ne trouve pas les commandes");
    return;
  }

  jsfile.forEach((f, i) =>{
    let props = require(`./commands/${f}`);
    console.log(`${f} chargée!`);

  });

  fs.readdir("./events/", (error, f) => {
    if (error) console.log(error);
    console.log(`${f.length} events en chargement`);
  })


共1个答案

匿名用户

fs.readdir是一个异步函数,它返回错误或目录中的文件列表。 您需要检查是否返回了错误或找到了文件,只有在没有返回错误的情况下才继续处理返回的数据。 阅读更多nodejs fs。 试试这个

  fs.readdir("./events/", (error, f) => {
    if (error) {
       console.log(error);

    } else {
       // only check length if no errors
       // otgherwise f is null and we can not call f.length
       console.log(`${f.length} events en chargement`);
    }

  })