I am very new to nodejs and am not able to figure out what exactly is going on here-
i have a basic login page, in which as the user types in the values, i go to the home page, before which i check up on the database if that username exists or not. The problem i am having is that when i click on the submit button, the code never seems to reach the database function.
This is my module in app.js -
app.post('/home',function(req,res){
//check for user existence....
var str;
console.log("in home");
user_login.perform_login(req.body.username,function(err,str){
if(!err){
console.log(str+" in login");
var cookie_val=req.body.username;
res.cookie('username',cookie_val,{signed: true});
req.session.username=cookie_val;
res.render('home.ejs',{
layout: false,
locals: {varname: cookie_val},
udata: req.session.username
});
}
else{
console.log("Couldn't find it.");
res.redirect("/login");
}
});
});
This is the perform_login from user_login.js-
var mongo_db=require("./testdb.js");
exports.perform_login=function (username,callback){
mongo_db.getInfo(username,function(err,bundle){
if(!err){
//console.log("success\n"+bundle);
callback(false,bundle);
}
else{
//console.log("couldn't find it");
callback(true,null);
}
});
}
and this is the testdb.js-
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/test');
exports.getInfo=function(value,callback){
var db=mongoose.connection;
db.on('error',console.error.bind(console,'connection error:'));
db.once('open',function(){
console.log("Connected!");
var userSchema=mongoose.Schema({
username: String,
password: String
});
var userInstances=mongoose.model('userInstances',userSchema);
userInstances.findOne({username: value},'username password',function(err,ui){
if(!err){
callback(false,ui);
}
else{
callback(true,"error");
}
});//findone
});//db.once function
}//getinfo
In the terminal i only get -"in home", after which nothing happens at all.In the browser, it seems to be processing but remains in the login page.
when i run the perform_login with an actual hard-coded value instead of req.body.username, immediately as app.js is launched, i get the proper results. So i am able to access the database somehow outside app.post().
Is there something wrong in the callbacks??
I think that you are doing a bit too much here. The symptoms you are describing are in my experience from a not connected mongoose. You do not need to manage the connection and listen on events, if it is connected. Mongoose handles this for you. You can query a model and it will return the result as soon the connection is established.
Here is an example that should work:
app.js
//initialize mongo connection
var mongooseConnection = mongoose.connect("mongodb://localhost/test");
user.js
var mongoose = require('mongoose');
var userSchema=mongoose.Schema({
username: String,
password: String
});
userSchema.statics.performLogin = function(username, callback) {
this.findOne({username: value},'username password',function(err,user){
if(!err){
callback(false,user);
}
else{
callback(true,"error");
}
});
module.exports = mongoose.model('users',userSchema);
route:
var User = require("./user");
app.post('/home',function(req,res){
User.performLogin(req.body.username, function (err, user) {
...
});
});