1

I was working on a project on windows on NodeJS.

I used the following code for checking login credentials. It was working fine till I changed to ubuntu 16.04 .

I set up my environment and now the variables are not being treated as strings.

It's returning empty set from the DB. If I do it like this user_name:"some user name",password:"some password", it's working fine.

Please help. I am stuck bad :-( sorry if its a noob type mistake :-)

app.post('/login', function(req, res, next) {
    var N1 = req.body.N1;
    var N2 = req.body.N2;
    console.log(N1+N2);//Getting passed successfully
    MongoClient.connect(url, function(err, db) {
        if (err) {
            throw err;
            console.log("into connectivity error");
        }
        console.log(N1);
        db.collection('col.login').find({user_name:N1,pass:N2}).toArray(function(err, result) {
            if (err) {
                throw err;
            }
            resultArray=result;
            try{
                req.session.userId = result[0]._id;
                req.session.userType = result[0].type;
                req.session.userComp = result[0].comp_id;
                req.session.cat = -1;
                if(result[0].type=="root") {
                    res.redirect('/New_Company');
                }

database structure:

{
    "_id" : ObjectId("xxxxxxxxxxxxxxxxxxx"),
    "user_name" : "xxxxxxx",
    "pass" : "xxxxx",
    "type" : "root",
    "email" : "[email protected]",
    "comp_name" : "NULL",
    "default_page" : "main",
    "name" : "xxxxx",
    "comp_id" : "NULL"
}
8
  • What value you are getting for console.log(N1); ? Commented Jun 7, 2016 at 7:54
  • i checked both N1 and N2 they are what i posted from the login page Commented Jun 7, 2016 at 7:58
  • Can you share your db structure? Commented Jun 7, 2016 at 7:59
  • sure give me a moment Commented Jun 7, 2016 at 8:00
  • Not sure why it is not working. Can you try doing toString() for N2 and N1 and see if it is working? Commented Jun 7, 2016 at 8:05

1 Answer 1

1

Do the following way :-

app.post('/login', function(req, res, next) {
var N1 = (req.body.N1).toString();//Do toString()
var N2 = (req.body.N2).toString();
console.log(N1+N2);//Getting passed successfully
MongoClient.connect(url, function(err, db) {
    if (err) {
        throw err;
        console.log("into connectivity error");
    }
    console.log(N1);
    db.collection('col.login').find({user_name:N1,pass:N2}).toArray(function(err, result) {
        if (err) {
            throw err;
        }
        resultArray=result;
        try{
            req.session.userId = result[0]._id;
            req.session.userType = result[0].type;
            req.session.userComp = result[0].comp_id;
            req.session.cat = -1;
            if(result[0].type=="root") {
                res.redirect('/New_Company');
            }
      });

Refer toString for more info on toString().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.