【问题标题】:minLength property in User model not working用户模型中的 minLength 属性不起作用
【发布时间】:2019-05-17 02:34:02
【问题描述】:

除了密码的 minLength 属性外,一切都运行良好。

如果我从 Postman 发送 { "email" : "harshit@example.com", "password": "abc" }, 即使我将 minLength 设置为 6,它仍然有效。

电子邮件的 minLength 属性工作得很好,但密码却不行。

server.js

app.post('/users', (req, res) => {
    var body = _.pick(req.body, ['email', 'password']);
    var user = new User(body);

    user.save().then((doc) => {
        res.send(doc)
    }).catch( (err) => {
        res.send(err)
    })
});

user.js // 在这里使用猫鼬。

var User = mongoose.model('User', {
    email: {
        type: String,
        required: true,
        trim: true,
        minLength: 5,
        unique: true,
        validate: {
            validator: validator.isEmail ,
            message: `{VALUE} is not a valid E-Mail`
        }
    },

    password: {
        type: String,
        required: true,
        minLength: 6, // This line isn't working
        trim: true
    },

    tokens: [{
        access: {
            type: String,
            required: true
        },
        token: {
            type: String,
            required: true
        }
    }]
});

【问题讨论】:

  • 您是否在编译第一个模型后添加了此选项?如果是这样,您需要将其删除并重新编译

标签: node.js database mongodb mongoose backend


【解决方案1】:

根据Mongoose documentation,您应该使用minlength,注意小写l

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2013-05-30
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多