无法使用ejs模板在视图中显示错误消息

问题描述 投票:0回答:1

我是node.js的新手。我无法使用ejs模板在视图中显示错误消息。我想显示此用户已存在。这是我的代码:

node.js

router.post('/signup', (req, res) => {

    var username = req.body.username;
    var email = req.body.email;
    var password = req.body.password;
    let err_msg = '';

    db.query('SELECT * FROM user_data WHERE username = ? and email = ?',  [username,email],(err,rows) => {

        if(err) 
        {
            err_msg = "Some error occured , plz register again.";
            return res.render('signup', { err_msg: err_msg } );
        }

        if (!rows.length)
        {
            db.query('INSERT INTO `user_data`(`username`,`email`,`password`) VAlUES (?, ?, ?)', [username,email,password], (err, results) => {
        return res.redirect('login'); 
        });

        }
        else
        {
            err_msg = "This user already exists.";
            return res.render('signup', { err_msg: err_msg } );
        }
    });
});

HTML代码

<div class="alert alert-danger" role="alert"><%= err_msg %></div>
javascript mysql node.js ejs registration
1个回答
0
投票
<% if(locals.err_msg) { %>
<div class="alert alert-danger" role="alert"><%= err_msg %></div>
<% } %>
© www.soinside.com 2019 - 2024. All rights reserved.