在NodeJS中获得未处理的承诺拒绝警告

时间:2020-05-13 19:26:17

标签: javascript node.js express promise async-await

我想知道为什么我的代码看起来还可以,但是为什么我得到这个错误/警告。

这是我开始构建的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世界的新手。

1 个答案:

答案 0 :(得分:1)

就像评论中说的那样,您应该在Azure App Service deploy中的try周围放置catch / await。像这样:

getAll
相关问题