为什么我的mongoose架构没有验证?

问题描述 投票:0回答:1

我试图用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个字符,所以它不应该工作,但确实如此。有人能帮我吗?

validation vue.js mongoose
1个回答
1
投票

您已使用了验证器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 }
});

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.