A promise to reject does not work with mysql error

I try to get mysql data using promises and it works, but when I try to create an error in mysql specifically, but then I get this error.

(node:28464) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Users not found
(node:28464) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I checked everywhere and he says he uses .catch, but it is strange that I use catch, but I can not understand the problem. Usually I would like to know how to solve this, and not just continue without knowing the right path.

This is a model.

getAllUser =
     new Promise((resolve, reject) => {
        db.query('SELECT * from users', function (error, results, fields) {
            if (error){
                return reject('Users not found');
            }else{
                resolve(results[0]);
            }
        });
    });
module.exports = {
    getAllUser
};

And this is how I call the model, the model at the top should return an error, since the mysql table is called usernot users.

router.get('/db',(req,res,next)=>{
    getAllUser.then((result)=>{
      console.log(result);
    }).catch((e) => {
       console.log(e);
    });
});
+4
source share
1 answer

. , , Promise.

:

:

getAllUser = () => new Promise((resolve, reject) => {
    db.query('SELECT * from users', function (error, results, fields) {
        if (error){
            return reject('Users not found');
        }else{
            resolve(results[0]);
        }
    });
});
module.exports = {
    getAllUser
};

Router:

router.get('/db',(req,res,next)=>{
    getAllUser.then((result)=>{
      console.log(result);
    }).catch((e) => {
       console.log(e);
    });
});
+1

Source: https://habr.com/ru/post/1685358/


All Articles