如何解决 UnhandledPromiseRejectionWarning?

How to resolve UnhandledPromiseRejectionWarning?

你好,美好的一天。

我有问题。我可以注册一个用户,但是当我登录该用户时,它会永远加载并显示:"UnhandledPromiseRejectionWarning: ReferenceError: Invalid left-hand side in assignment"

所以这是我的用户的内容table。

用户结构Table

UnhandledPromiseRejection 错误

基本上,它说我的 controllers/auth.js 有问题,在第 30 行。

这是我在 auth.js 上的完整代码。

exports.signin = (req, res) => {
    try {
        const {email, password} = req.body;
        if(!email || !password) {
            return res.send("<script> alert('Provide an email and/or Password'); window.location='/signin'; </script>");
        }
        con.query('SELECT * FROM users WHERE email = ?', [email], async (error, results) => {
            console.log(results);
            if(!results || !(await bcrypt.compare(password, results[0].password))) {
                return res.send("<script> alert('Email or Password is incorrect'); window.location='/signin'; </script>");
            }
            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("/profile");
            }
        });
    }
    catch(error) {
        console.log(error);
    }
}

exports.signup = (req, res) => {
    console.log(req.body);

    const {name, email, password, passwordConfirm} = req.body;
    con.query('SELECT email FROM users WHERE email = ?', [email], async (error, results) => {
        if(error) {
            console.log(error);
        }
        if(results.length > 0) {
            return res.send("<script> alert('This email is already in use or invalid.'); window.location='/signup'; </script>");
        }
        else if(password !== passwordConfirm) {
            return res.send("<script> alert('Passwords do not match.'); window.location='/signup'; </script>");         
        }

        let hashedPassword = await bcrypt.hash(password, 8);
        console.log(hashedPassword);

        con.query('INSERT INTO users SET ?', {name: name, email: email, password: hashedPassword}, (error, results) => {
            if(error) {
                console.log(error);
            }
            else {
                console.log(results);
                return res.send("<script> alert('USER IS REGISTERED! You are redirected to Sign-in Page.'); window.location='/signin'; </script>");      
            }
        });
    });
}

我看了他的频道,复制了代码 https://www.youtube.com/watch?v=VavWEtI5T7c&ab_channel=TelmoSampaio

问题出在下面一行:

expires: new Date(Date.now() = process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),

您正在尝试分配一个值,但它可能应该是在当前日期上加上过期时间:

expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES * 24 * 60 * 60 * 1000),