运行节点服务器时,出现类型错误:无法读取未定义的属性“名称”。直到我console.log(“ tt3” + user_data.name),我才能console.log user_data.name。然后它只是说tt3undefined。
即使我能够console.log user_data.name,我仍然无法读取第一个um.getUser()中未定义的属性“名称”;
我完全不知道问题是什么。
我相信我的功能也能正常运行。
当我console.log(user_data.name)时,它将打印名称
router.get('/users/game', function(req, res){
var user_data = {
name: req.query.player_name,
password: req.query.password
};
console.log("does this exist"+user_data.name);
if(user_data.name !=="" && user_data.password !== ""){ //if the password and username aren't blank
//console.log(user_data.name);
um.getUser(user_data.name, user_data.password, function(response){
var userinfo = response;
exports.logplayer = userinfo;
//console.log("tt" +user_data.name);
//console.log(exports.logplayer);
if(exports.logplayer == null){ //if the username and password dont exist
// console.log("tt2" +user_data.name);
um.getUsername(user_data.name,function(response){ //check to see if username exists
console.log("tt3" +user_data);
var userinfo = response;
exports.logplayer = userinfo;
if(exports.logplayer == null){ //if user name doesnt exist then create a new user
var newuser = new Player(user_data.name,0,0,0,0,0,0,user_data.password, "", "");
console.log(JSON.stringify(newuser) + "user couldn't be found in csv, so new user has been created");
um.updateUser(newuser,function(){ //this will update to csv
exports.logplayer = newuser;
res.status(200);
res.setHeader('Content-Type', 'text/html')
res.render('game', {user:exports.logplayer});
}); //updateUser
}
else{ //if username does exist, return index for them to input the correct password
var user_data = {
name:"",
password:"",
message:"The entered username exists but the entered password doesn't match it."
};
res.status(200);
res.setHeader('Content-Type', 'text/html')
res.render('index', {data:user_data});
console.log("wrong password");
}
});// getusername
}
else{ //if the player already exists
res.status(200);
message = "Let's play!";
res.setHeader('Content-Type', 'text/html')
res.render('game', {user:exports.logplayer, message:message});
console.log("rendering the game for an existing user:" + exports.logplayer.name);
}
});//getUser callback
} //password/username isn't blank
else{ // if password or username is blank
var user_data = {
name:"",
password:"",
message:"Please enter a username and password."
};
console.log(message);
res.status(200);
res.setHeader('Content-Type', 'text/html')
res.render('index', {data:user_data});
}
});//router.get('/users/game'