Im trying to implement a simple login mechanism from NodeJS(using ExpressJS) and MongoDB. Im using MongoJS for the DB connectivity. I am using the $and:
to see if the fields match inside the collection.
function authenticate(req,res){
username = req.body.username;
password = req.body.password;
db.users.find({$and :[{username:username},{password:password}]},function(err,doc){
if(( Object.keys(doc).length === 0 && doc.constructor === Object) === false){
res.send("Invalid login")
} else {
doc.forEach(function(doc){
console.log(doc.firstname)
})
}
})
}
Im checking if the doc contanis an empty object (which means the username and password didnt match) and telling the page to show that the login is invalid. If the doc does contain a matching username and password, Im console.log()
ging the firstname of the user...
The code above is not working...What is it that I am doing wrong?
Thanks in advance...