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

How to display the error in .ejs template

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

So I converted the .hbs template to .ejs template and try to see the difference. I thought converting would be same, but it turns out it doesn't.

My code is working. I can register a user, but no error shows up. The error display is the main problem in my code.

Error-looping of .ejs

This is the .HBS version of Error

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

This is the .EJS version of Error

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

Another version of Error in .EJS

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

This is the Controllers folder - 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!'
                });   
            }
        });
    });
}

This is the Routes folder - user.js

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

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

Lord You can solve this job in two ways.

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

2- If you don't want to use local messages Use 'flash' and 'session' packages