【问题标题】:Mongoose validators not updatedMongoose 验证器未更新
【发布时间】:2021-01-06 21:39:52
【问题描述】:

我正在使用 mongoose 和 npm 包 mongoose-unique-validator 进行验证。

插入我的架构时它工作得很好,但我将验证从一个属性更改为另一个。现在我仍然收到电子邮件的旧验证(MongoError 11000)的唯一 mongo 错误......虽然我将它移到用户名,好像之前的验证器没有被删除并且没有正确更新。

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;

let userSchema = new Schema({
    username:{
        type: String,
        required: true,
        unique: true,
        uniqueCaseInsensitive: true
    },
    email: {
        type: String,
        required: true
    }
}
userSchema.plugin(uniqueValidator, {message : 'username must be unique'});

module.exports.User = mongoose.model('User', userSchema);

有没有办法更新验证器?我还没有找到任何东西

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    请完全放弃您的收藏,然后重新运行您的项目。之后一切都会好起来的,因为根据您的电子邮件在您的 mongodb 中创建的唯一索引仍然存在......

    对于删除索引,您可以使用用于 mongodb 的 IDE,例如 robo 3t 或 ... 使用命令可以删除索引

    db.getCollection('CollocationName').dropIndex( "indexName_1" )
    

    对你来说是:

    db.getCollection('users').dropIndex( "email_1" )
    

    如果索引是由 mongoose 创建的,你应该使用 indexName 和 _1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-06
      • 2021-03-23
      • 2020-08-26
      • 2014-02-15
      • 2019-05-10
      • 2022-01-15
      • 2017-08-22
      • 2023-03-27
      相关资源
      最近更新 更多