我试图用mongoose架构验证我的数据。但它不起作用,我不知道为什么。
这是我的架构
var mongoose = require("mongoose");
var UserSchema = new mongoose.Schema({
username: { type: String, min: 3, max: 30, required: true },
password: { type: String, min: 6, required: true }
});
mongoose.model("User", UserSchema);
这是我称之为帖子的地方
router.post('/signup', (req, res) => {
const user = new User({
username: "Marcel",
password: "12345"
})
user.save().then(function(){
res.json({
message: '✅'
})
}).catch(function(){
res.json({
message: '❌'
})
})
})
我给了密码至少6个字符,但是对于示例用户,我给了5个字符,所以它不应该工作,但确实如此。有人能帮我吗?
您已使用了验证器min和max,它们是Number类型。
尝试使用minlength和maxlength代替String类型:
var UserSchema = new mongoose.Schema({
username: { type: String, minlength: 3, maxlength: 30, required: true },
password: { type: String, minlength: 6, required: true }
});
我希望这有帮助。