Reputation: 1078
Im trying to get the value of a field located in a document stored in a mongodb collection. My code is not working and I'm not sure why. I know find() returns a cursor object, which I want to traverse using cursor.next(). It seems that point in the code is where it's failing based on my debugging. I'm using the mongodb module for node.js to perform these operations. Here is the code:
Setting up the DB/collection info:
exports.login = function(request, response){
var Db = require('mongodb').Db,
Server = require('mongodb').Server,
assert = require('assert'),
server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true}),
udb = new Db('users', server_config, {
w: -1
}),
uname = request.body.uName,
pw = request.body.uPass;
The opening the db and querying:
udb.open(function(err, udb) {
assert.equal(null, err);
var collection = udb.collection('profiles');
collection.find({username: uname, password: pw}, function(err, collection){
if( err || !collection){
console.log("login fail");
response.redirect('/');
}
else {
collection.each(function(err, result){
assert.equal(null, err);
console.log("login success");
request.session.loggedIn = true;
request.session.user = uname;
response.redirect('/userDashboard');
});
}
});
udb.close();
});
}
I don't seem to be getting any errors, the page just 'hangs' indefinitely.
Upvotes: 0
Views: 147
Reputation: 2206
I think nothing's being found. Test the query in the mongo shell and see if you're getting anything. Also be sure not to store passwords in plain text! Check out the node bcrypt package.
Upvotes: 1