How to get user info from mongodb in node.js
Totally new to mongo, I've been checking examples for hours, Trying to
check if a user exists in this collection:
{ "name" : "chrispy", "pass" : "xxxx", "_id" :
ObjectId("5221b29b69f9e9b11a000001") }
But cannot match name and get the results, ive tried numerous examples,
and no luck. onRegister is called and data.name and data.password are
filled. Once I can match the name, I'll match the password. but cant even
get as far as matching the user name. console.log doesn't get called
either. Help = 1000 thankyou's!
function onRegister(data) {
var name,pass;
name = data.name;
pass = data.pass;
Mongo.connect('mongodb://127.0.0.1:27017/main', function(err, db) {
if(err) throw err;
var collection = db.collection('users');
// check if user exists
var doc = collection.findOne({'name' : name}, function(err,doc){
if(err) throw err;
if(doc) {
var myName = doc.name;
console.log("doc.name "+tojson(myName)+ " name "+name); //
never called even when name is correct
if(tojson(myName) == name) {
// now check password, but runtime never gets here
}
} else {
// runtime never gets here either
}
});
}
No comments:
Post a Comment