如果-else条件在node js中不起作用。

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

我想实现的是,在登录后,系统会检查用户的角色,并相应地重定向用户。1- Admin2-用户角色字段是整数类型。

router.post('/signin', function (req, res, next) {
    session_store = req.session;
    req.assert('Emailid', 'Please fill register Email Address').notEmpty();
    req.assert('Emailid', 'Email not valid').isEmail();
    req.assert('password', 'Please fill the Password').notEmpty();
    var errors = req.validationErrors();
    if (!errors) {

            Emailid = req.sanitize('Emailid').escape().trim();
            password = req.sanitize('password').escape().trim();

        var query = 'select * from userdetails where Emailid=? and password=?';
        var sql='select role from userdetails where Emailid=?'
            db.query(query, [Emailid, password], function (err, rows) {

                if (err) {

                    var errornya = ("Error Selecting : %s ", err.code);
                    console.log(err.code);
                    req.flash('msg_error', errornya);
                    res.redirect('/login-Form');
                } else {
                    if (rows.length <= 0) {

                        req.flash('msg_error', "Wrong email address or password. Try again.");
                        res.redirect('/login-Form');
                    }
                    else {
                        session_store.is_login = true;
                        session_store.user = Emailid;
                        db.query(sql, Emailid, function (err, result) {
                            if (err) throw err
                            else {

                                if (result == 1) { // facing issue here. It is directly going to else block though the user role is 1 in the mysql table
                                    console.log(result)
                                    res.redirect('/Dashboard');
                                }
                                else {

                                    res.redirect('/Audit-Record');
                                }

                            }
                        });


                    }
                }

            });
        }

    else {


        res.redirect('/login-Form');
    }
});

我想我在比较结果值的时候犯了一些错误。你们谁能帮我检查一下,让我知道我哪里做错了。

mysql node.js express handlebars.js basic-authentication
1个回答
0
投票

问题解决了。其实我的比较方式是错误的,我们需要这样写。

else {
                        session_store.is_login = true;
                        session_store.user = Emailid;
                        db.query(sql, Emailid, function (err, result) {
                            if (err) throw err
                            else {

                                **if (result[0].role == 1)** {
                                    console.log(result)
                                    res.redirect('/Dashboard');
                                }
                                else {

                                    res.redirect('/Audit-Record');
                                }

                            }
                        }); 
© www.soinside.com 2019 - 2024. All rights reserved.