Reputation: 2893
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
Upvotes: 3
Views: 160
Reputation: 19569
I believe your username is stored, but not in the object, it's in the key itself. Here's a quick node console test that shows it:
zlatko@zlatko-desktop ~/projects/node-app $ node
> var userDB = {};
undefined
> var username = 'tester';
undefined
> userDB[username] = {count: 0, password: 'myPass'};
{ count: 0, password: 'myPass' }
> userDB;
{ tester: { count: 0, password: 'myPass' } }
> userDB[username];
{ count: 0, password: 'myPass' }
> userDB.tester
{ count: 0, password: 'myPass' }
> userDB.hasOwnProperty('tester');
true
>
So the object actually is saved. One thing that comes in mind is that either your database is not as global as you think, or it is not persistant. Do you create it each time you run the app or each time you run the request?
Also, a tip. I fondly suggest something like passport.js for this kind of stuff. It's bliss, solves all this stuff for you, and adds a nice properties and methods like req.isAuthenticated() and req.user to the req object.
Upvotes: 1
Reputation: 8206
well, what is redirect doing? if you redirect then you lose the info. also, try
console.log("added new user: ", username, "with properties: ", JSON.stringify(userDatabase[username]));
Upvotes: 1