Why do I get this error?When I don't have the Cnic field in my DB anymore.
Here is my schema....
const mongoose = require('mongoose')
const uuidv1 = require("uuidv1")
const crypto = require("crypto")
const { ObjectId } = mongoose.Schema
const clientSchema = new mongoose.Schema({
name:{
type:String,
trim: true,
required: true,
// match:[
// new RegExp('^[a-z]+$', 'i'),
// 'Name Should have alphabets'
// ]
},
phone:{
type: String,
trim: true,
required: true,
unique: true
},
email:{
type: String,
trim: true,
required: true,
unique:true
},
hashed_password:{
type: String,
required: true
},
salt:String,
created:{
type: Date,
default: Date.now
},
createdBy:{
type: ObjectId,
ref: "Admin"
},
updated: Date
})
clientSchema.virtual('password')
.set(function(password){
this._password = password
// generate a timestamp
this.salt = uuidv1()
// encrypt password
this.hashed_password = this.encryptPassword(password)
})
.get(function(){
return this._password
})
clientSchema.methods = {
authenticate: function(plainText){
return this.encryptPassword(plainText) === this.hashed_password
},
encryptPassword: function(password){
if(!password) return "";
try{
return crypto
.createHmac("sha1", this.salt)
.update(password)
.digest('hex');
}catch (err){
return ""
}
}
}
module.exports = mongoose.model("Client", clientSchema)
I have made some changes , before this I had the Cnic field which was Unique...I have deleted that field.When I create a first user it gets created successfully but when I create a second user it gives the error above in the title...What is happening and how to solve it..?
------------------------SOLUTION----------------------------------------
I dropped a collection in my DB and it worked for me. Detailed answer is available in the comment section provided my @Scott Gnile