For organization purposes I'm separating my source code into modules, for example I have the user
module on my node.js app which is responsable for retrieving user information from a MongoDB database. I'm doing something like this:
var mongo = require("mongodb"),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server("localhost", 27017, { auto_reconnect: true });
var db = new Db("users", server);
module.exports = {
login: function(user, pass, callback) {
var reg_result = null;
db.open(function (err, db) {
if(!err) {
db.collection("users", function(err, collection) {
collection.findOne(
{
"username": user,
"password": pass
},
function(err, item) {
if(!err) {
reg_result = item;
} else {
reg_result = "error";
}
}
);
});
} else {
reg_result = "error";
console.log("ERROR: " + err);
}
});
callback(reg_result);
}
}
And executing it on my test script like this:
var user = require("./user.js");
user.log("test", "test", function(msg) {
console.log(msg);
});
It does the database operation and retrieves the value, but every time it only returns null
, when I don't initialize the reg_result
variable it returns undefined
. What should I do to correct this?
I debugged using console.log
on the user.js
and the item
was outputted, but I want to have the callback so I can use the item on other sources, like my test script