I have a global object defined like this:
var userDatabase = {};
In my application, I am trying to register new users. This is what my function looks like:
function register(req, res){
var username = req.body.username;
var obj= {count: 0,
password:req.body.password};
//if user is already in the database
if(userDatabase.hasOwnProperty(username)){
res.redirect("/?error=User has already registered");
}
//store user, redirect to home page
else{
userDatabase[username] = obj;
console.log("added new user: ", username, "with properties: ", userDatabase[username]);
//redirect to homepage
res.redirect("/");
}
}
My problem is that I don't believe that saying :
userDatabase[username] = obj;
is actually adding my newly registered user into my object because when I print out to my console, it says that my username is undefined.
How can I properly store my username in my object? Thanks!
Edit: I should add that I can only register one user with this code, when I try to register a second user, it presents the "user has already registered" error... which makes me REALLy believe that my object is not doing what I want it to do
This is my console log when I print out the value of userDatabase[username] before and after the if/else statement:
Express server listening on port 3000
before if/else: undefined
added new user: undefined with properties: { count: 0, password: 'abc' }
after if/else: { count: 0, password: 'abc' }
POST /register 302 11ms - 58b
GET / 200 226ms - 803b
GET /libs/jquery/jquery.min.js 304 5ms
GET /libs/bootstrap/css/bootstrap.min.css 304 4ms
GET /stylesheets/style.css 200 55ms - 110b
before if/else: { count: 0, password: 'abc' }
after if/else: { count: 0, password: 'abc' }
POST /register 302 2ms - 126b
GET /?error=User%20has%20already%20registered 200 27ms - 861b
GET /libs/jquery/jquery.min.js 304 2ms
GET /libs/bootstrap/css/bootstrap.min.css 304 1ms
GET /stylesheets/style.css 304 2ms
There is something very wrong here - not recognizing new user I have tried to add
EDIT 2:
when I try to access the userDatabase[username] in another function in the same file, it is not able to verify that the userDatabase{} has this user stored in it.This is how I am trying to access it:
function login(req, res) {
var username = req.body.username;
var password = req.body.password;
req.session.username = username;
req.session.password = password;
loggedInUsers[username] = LoggedIn;
//when login button is pressed
if(userDatabase.hasOwnProperty(username) ){
storedPassword = userDatabase[username].password;
if (storedPassword === password){
//check for correct password
userDatabase[username].count++;
console.log("Count: ", userDatabase[username].count);
res.redirect("/users");
}
}
else{
res.redirect("/?error=Invalid username/password");
}
}
and the console output after registering and then trying to log in:
Express server listening on port 3000
before if/else: undefined
added new user: undefined with properties: { count: 0, password: 'abc' }
after if/else: { count: 0, password: 'abc' }
POST /register 302 11ms - 58b
GET / 200 232ms - 803b
GET /libs/jquery/jquery.min.js 304 6ms
GET /libs/bootstrap/css/bootstrap.min.css 304 6ms
GET /stylesheets/style.css 200 69ms - 110b
POST /login 302 2ms - 122b
GET /?error=Invalid%20username/password 302 1ms - 68b
GET /users 200 22ms - 430b
GET /libs/jquery/jquery.min.js 304 2ms
GET /libs/bootstrap/css/bootstrap.min.css 304 3ms
GET /stylesheets/style.css 304 1ms