我有一个Web服务器,我正在尝试学习如何使用mongoDB和node.js创建登录和注册页面。我的登录请求调用函数checkUser,它应该检查用户登录的用户名和密码。当我尝试使用用户名和密码登录时,我知道在我的数据库中我收到错误错误:TypeError:不能读取未定义的属性“用户名”
我的checkUser功能:
function checkUser(userData) {
return User.find({ userName: userData.userName }).exec().then((user) => {
if (user == undefined || user.length == 0) {
throw new Error("Unable to find user: " + userData.userName);
}
else if (user[0].password != userData.password) {
throw new Error("Incorrect Password for user: " + userData.userName);
}
else if (user[0].password == userData.password)
{
user[0].loginHistory.push({
dateTime: (new Date()).toString(),
userAgent: userData.userAgent});
user[0].update({
userName: userData.userName}, {$set: {loginHistory: user[0].loginHistory}}).exec().then((user) => {
return user[0];
})
.catch((err) => {
console.log(err);
console.log(err.stack);
throw new Error("There was an error verifying the user: ${err}");
})
}
}).catch((err) => {
console.log(err);
console.log(err.stack);
throw new Error("Unable to find user: " + err);
});
};
我的架构:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var userSchema = new Schema({
"userName": {
unique: true,
type: String
},
"password": String,
"email": String,
"loginHistory": [{ "dateTime": Date, "userAgent": String }]
});
let User; //to be defined on new connection
我可以看到数据库中的用户我似乎无法弄清楚如何让他们能够登录。