我有一个像这样定义的全局对象:
var userDatabase = {};
在我的应用程序中,我正在尝试注册新用户。这就是我的功能:
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("/");
}
}
我的问题是我不相信这样说:
userDatabase[username] = obj;
实际上是将我新注册的用户添加到我的对象中,因为当我打印到我的控制台时,它说我的用户名是未定义的。
如何在用户对象中正确存储用户名?谢谢!
编辑:我应该补充一点,我只能用这个代码注册一个用户,当我尝试注册第二个用户时,它会显示“用户已经注册”错误...这让我真的相信我的对象是不做我想做的事
当我在if / else语句之前和之后打印userDatabase [username]的值时,这是我的控制台日志:
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
这里有一些非常错误 - 没有识别我试图添加的新用户
编辑2:
当我尝试访问同一文件中另一个函数中的userDatabase [username]时,它无法验证userDatabase {}是否已将此用户存储在其中。这就是我尝试访问它的方式:< / p>
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");
}
}
注册然后尝试登录后,和控制台输出:
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
答案 0 :(得分:1)
嗯,重定向是做什么的?如果你重定向,那么你就失去了信息。另外,试试
console.log("added new user: ", username, "with properties: ", JSON.stringify(userDatabase[username]));
答案 1 :(得分:1)
我相信您的用户名已存储,但不存在于对象中,而是存在于密钥本身中。这是一个快速节点控制台测试,显示它:
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
>
所以对象实际上已经保存了。要记住的一件事是,您的数据库不是您想象的全局,或者它不是持久的。您是在每次运行应用程序时还是每次运行请求时创建它?
另外,小费。我亲切地建议像passport.js这样的东西。这是幸福,为您解决所有这些问题,并为 req req.isAuthenticated()和 req.user >对象。