Cannot read property 'collection' of undefined while connecting to mongodb in node.js?
23:39 25 Oct 2015

I am trying to login from my signin.ejs file,after click on the login button in my signin page it shows "Cannot read property collection undefined" what is the problem.

I defined my route like this in app.js:

app.post('/login', routes.dologin);

I defined my dologin route in index.js:

exports.dologin = function (req, res) {
res.locals.session = req.session;

var user = req.body.user;
db.authenticateUser(user.email, user.password, function     ( err, response) {
if (err) {
.......

.......
} else {
.......

........  
}
});
};

In my db.js:

var mongo = require('mongoskin'),
crypto = require('crypto');

module.exports = function (config) {

var USERS_COLLECTION = 'users',
ORDERS_COLLECTION = 'orders',
salt = 'supersecretkey',
db;

    authenticateUser: function (emailId, password,         callback) {
    db.collection(USERS_COLLECTION).count({email : emailId,         password: encryptPassword(password)}, function (err,         count) {
    if (err) {
console.log("error authenticating user: " + err);
callback(new Error(err));
} else if (count === 0) {
callback(new Error("emailid/password did not match"));
} else {
callback(null);
}
});
},
    }

What is the problem here for getting "Collection undefined"? i think here everything is right... Is there any problem here?tell me..please Thanks.

node.js mongodb