arvindr21 / diskDB

A Lightweight Disk based JSON Database with a MongoDB like API for Node
http://arvindr21.github.io/diskDB
603 stars 65 forks source link

How to use multiple condition in find? #33

Closed BrstDev123 closed 7 years ago

BrstDev123 commented 7 years ago

In users.json data is [{"email":"noname@email.com","password":"123","status":"1","_id":"e39a62a9c7b94d76892f9c3bdfa22715"}]

I am using it as follows:

        db.loadCollections(['users']);

        var preCheck = db.users.find({"email" : req.body.email,"password" : req.body.password});

        if(preCheck !== undefined){

            if( (preCheck.email == req.body.email) && (preCheck.password == req.body.password) ){ 
                //logged in

                req.session.email = req.body.email;
                req.session.userId = preCheck._id;
                req.flash('success','Successful Login');
                res.redirect('/option');
            }
            else{

                req.flash('error','Email Id or Password is Incorrect');
                res.redirect('/login');
            }
        }

But it returns invalid login always.

staticmukesh commented 7 years ago

I think, syntax is correct for multiple conditions in search.

Try console.log(preCheck). You will find it to be array of matched results, not single object, that's why if condition is failing.

Also, why are you checking it again with the received values ?

Use findOne instead.

patchfx commented 7 years ago

@BrstDev123 You are getting back an array as @staticmukesh says, whereas it appears you are expecting a hash. You could either grab the first result in the array - though that's a little brittle, or use findOne.

var preCheck = db.users.findOne({"email" : req.body.email,"password" : req.body.password});

patchfx commented 7 years ago

@BrstDev123 I'm closing this issue, guessing it's solved for you now?

BrstDev123 commented 7 years ago

Well issue was not resolved , But I have replaced diskdb with sqlite3.. If you wants then close. Thank you!