【发布时间】:2021-01-02 23:58:48
【问题描述】:
class Main {
constructor() {
this.argument = process.argv.splice(2);
this.fileToCopy = this.argument[0];
this.destination = this.argument[1] ? this.argument[1] : '';
this.callAdress = process.cwd();
this.finalAdress = `${this.callAdress}\\` + this.destination;
//Problematic Part
if(this.fileExists(this.fileToCopy, this.finalAdress)) console.log("EXISTS")
else console.log("DOESNT EXISTS");
}
async fileExists(file, path) {
try {
let files = await fs.readdir(path);
return files.includes(file);
} catch(e) {
console.log("ERROR", e)
return false;
}
}
}
我试图检查目录中是否存在文件,使用 fs 的承诺,有问题的部分总是返回 true。我没有想法。
【问题讨论】:
-
您是否尝试过调试您的代码并逐行调试?
标签: javascript asynchronous fs