我做了一个简单的登录api,无论我发送什么作为参数,它都无法正常工作,这是我的用户架构:
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
username: {type: String, required: true , unique: true},
password: {type: String, required: true}
});
module.exports = mongoose.model('User', userSchema);
这是我的代码:
router.post('/login', (req,res,next) => {
var usern = req.body.username;
var passw = req.body.password;
console.log(usern + " " + passw);
if ( !usern || !passw ) {
return res.status(400).json({'error': 'Bad Request'});
} else {
User.findOne({username: req.body.username},(user , err)=> {
if (!user) {
return res.status(404).json({'error': 'No User Found'});
} else if (user.password !== req.body.password ) {
return res.status(401).json({'error': 'Auth Failed'});
} else {
return res.status(200).json({
'success': 'true'
});
}
});
}
});
提前致谢!