使用 nodejs 进行测试时获取 UnhandledPromiseRejectionWarning

Getting a UnhandledPromiseRejectionWarning when testing using nodejs

我正在测试 login/register 页面。虽然注册部分工作正常,但当尝试使用相同的用户凭据登录时,页面根本没有响应。 在控制台上,尽管使用了 try-catch 块,我还是收到 'UnhandledPromiseRejectionWarning'。

(node:8248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

(node:8248) [DEP0018] 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.

exports.login = async(req, res) => {
    try {
        const { email, password } = req.body;

        if (!email || !password) {
            return res.status(400).render('login', {
                message: "PLEASE PROVIDE AN EMAIL AND/OR PASSWORD"
            })
        }

        db.query('SELECT * FROM users WHERE email = ?', [email], async(error, results) => {
            console.log(results);
            if (!results || !(await bcrypt.compare(password, results[0].password))) {
                res.status(401).render('login', {
                    message: 'EMAIL OR PASSWORD IS INCORRECT'
                })
            } else {
                const id = results[0].id;

                const token = jwt.sign({ id }, process.env.JWT_SECRET, {
                    expiresIn: process.env.JWT_EXPIRES_IN
                });

                console.log("THE TOKEN IS " + token);

                const cookieOptions = {
                    expires: new Date(
                        Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000 
                    ),
                    httpOnly: true
                }

                res.cookie('jwt', token, cookieOptions);
                res.status(200).redirext("/");
            }
        })
    } catch (error) {
        console.log(error);
    }
}

任何形式的帮助都会有很大帮助。 谢谢!:)

首先你在发送响应时出错:

res.status(200).redirect("/");

注:x -> c

那你没有条件去检查bcrypt.compare是否会报错

试试下面的代码:

exports.login = async(req, res) => {
const { email, password } = req.body;

    if (!email || !password) {
        return res.status(400).render('login', {
            message: "PLEASE PROVIDE AN EMAIL AND/OR PASSWORD"
        })
    }

    db.query('SELECT * FROM users WHERE email = ?', [email], async(error, results) => {
        console.log(results);
        try {
            if (!results || !(await bcrypt.compare(password, results[0].password))) {
            res.status(401).render('login', {
                message: 'EMAIL OR PASSWORD IS INCORRECT'
            })
            } else {
                const id = results[0].id;

                const token = jwt.sign({ id }, process.env.JWT_SECRET, {
                    expiresIn: process.env.JWT_EXPIRES_IN
                });

                console.log("THE TOKEN IS " + token);

                const cookieOptions = {
                    expires: new Date(
                        Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000 
                    ),
                    httpOnly: true
                }

                res.cookie('jwt', token, cookieOptions);
                res.status(200).redirect("/");
            }
        } catch (error) {
            console.log(error);
        }
        
    })
}