Warm tip: This article is reproduced from serverfault.com, please click

node.js-如何在.ejs模板中显示错误

(node.js - How to display the error in .ejs template)

发布于 2020-12-08 08:22:34

所以我转换.hbs模板.ejs模板,并尝试看到了差距。我以为转换将是相同的,但事实并非如此。

我的代码正在运行。我可以注册一个用户,但是没有出现错误。错误显示是我代码中的主要问题。

.ejs的错误循环

这是.HBS版本的Error

{{#if message}} 
    <h4 class="alert alert-danger mt-4">{{message}}</h4> 
{{/if}}

这是Error的.EJS版本

//SignUp.js / SignIn.js
<% if (error) { %>
    <h4 class="alert alert-danger mt-4">{{message}}</h4>
<% } %>

.EJS中的错误的另一个版本

    <% if (hasErrors) {%>
        <div class="alert alert-danger">
            <% messages.forEach(function(message){ %>
                <p><%= message %></p>
            <% });%>
        </div>
    <% }%>

这是Controllers文件夹-auth.js

exports.signin = async (req, res) => {
    try {
        const {email, password} = req.body;
        
        if(!email || !password) {
            return res.status(400).render('shop/signin', {
                message: 'Please provide an email and/or password'
            });
        }
        
        con.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('shop/signin', {
                    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("shop/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.render('shop/signup', {
                message: 'That email is already in use!'
            });
        }
        else if(password !== passwordConfirm) {
            return res.render('shop/signup', {
                message: 'Passwords do not match!'
            });            
        }

        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.render('shop/signup', {
                    message: 'User Registered!'
                });   
            }
        });
    });
}

这是Routes文件夹-user.js

router.post('/signup', authController.signup);
router.post('/signin', authController.signin);

module.exports = router;
Questioner
Star-Lord
Viewed
11
irfan 2020-12-08 19:00:00

主你可以通过两种方式解决这项工作。

1-本地消息 return res.send("<script> alert('Error Message'); window.location = 'shop/signin'; </script>")

2-如果你不想使用本地消息,请使用“ flash”和“ session”程序包