validation - 为什么我的猫鼬模式没有验证?
问题描述
我正在尝试使用猫鼬模式验证我的数据。但它不起作用,我不知道为什么。
这是我的架构
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 个字符,所以它不应该工作,但它确实有效。有人能帮我吗?
解决方案
您已经使用了用于Number类型的验证器min和max 。
尝试使用minlength和maxlength代替,它们适用于String类型:
var UserSchema = new mongoose.Schema({
username: { type: String, minlength: 3, maxlength: 30, required: true },
password: { type: String, minlength: 6, required: true }
});
我希望这有帮助。
推荐阅读
- ionic-framework - 无法水平滚动离子卡
- jquery - jQuery - 实时更新长度,而不是仅在页面加载时
- wordpress - Wordpress 主题版权编辑(页脚)
- tfs - TFS 开发运维。找不到外部项目文件
- android - 一个文本视图,字符串最多再换行
- php - Amazon Connect 从 Lambda 函数和 PHP 接收 JSON 数据
- ios - 此 ActionScript 函数的 Swift 等效项
- c - getchar() 尝试扫描输入时跳过第一个字符
- objective-c - Mojave/macOS 10.14.0:[AVPlayerItem 持续时间] 始终是不确定的
- rest - 如何使用 Golang 实现导入数据功能?