关于schema。。我都干了些什么 = =
schema声明
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var User = new Schema({
username : String ,
password: String
});
var User = mongoose.model('User',User);
注册功能 输入的用户名是eee 密码是eee
//注册
app.post('/doregister',function(req,res){
console.log(req.body.user);// 输出{username:'eee',password:'eee'}
User.find({username:req.body.user.username},function(err,doc){
console.log(doc);
if (doc=[]){
console.log('can');//输出can
var user = new User(req.body.user);
user.save(function(err){
if(!err){
console.log('register success!');
console.log('user:'+req.body.user);//user [Object Object]
res.redirect('/index');
}else{
res.redirect('/register');
}
});
}else{
console.log('can t');
res.redirect('/register');
}
});
});
用eee & eee登陆的时候
app.post('/dologin',function(req,res){
User.find({username:req.body.user.username},function(err,doc){
if(doc.password = req.body.user.password){
console.log("登陆成功");
req.session.user = doc;
console.log(req.session.user);// 就是这个特别奇葩 输出是[{ _id:***********,username:'eee',password:'eee',__v:'0'},password:'eee']
res.redirect('/');
}else{
res.redirect('/login');
}
console.log(req.session.user.username);//undefined
console.log(req.session.user.password);//eee
});
});
what?!!why?!!
2 回复
===
写成了=
no~ 的确应该是 = 不信可以试验