我想知道为什么我的代码看起来还可以,但是为什么我得到这个错误/警告。
这是我开始构建的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世界的新手。
答案 0 :(得分:1)
就像评论中说的那样,您应该在Azure App Service deploy
中的try
周围放置catch
/ await
。像这样:
getAll