【发布时间】:2020-08-30 03:41:26
【问题描述】:
我想知道为什么我得到这个错误/警告,即使我的代码看起来没问题。
这是我开始构建的 UserModel:
const fs = require('fs');
class UserModel {
constructor(filename) {
if (!filename) {
throw new Error('Require a filename...');
}
this.file = filename;
try{
fs.accessSync(this.file); //to check if the file exists already
} catch (err) { //if not, make a new file
fs.writeFileSync(this.file, ['']);
}
}
async getAll() {
return JSON.parse(await fs.promises.readFile(this.file,{
encoding: 'utf8'
}));
}
}
//to test the above code
const test = async () => {
const user = new UserModel('users.json');
const data = await user.getAll();
console.log(data);
}
test();
请帮助,NodeJS 世界的新手。
【问题讨论】:
-
使用
test().catch(console.error),或在test函数中的getAll调用周围放置一个try/catch。 -
由于
test()函数而收到警告。
标签: javascript node.js express promise async-await