Home > Blockchain >  I am getting Duplication Key Error when I send a post request
I am getting Duplication Key Error when I send a post request

Time:03-03

I have a schema

const VendorSchema = new mongoose.Schema({
firstName: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide firstName"],
    trim: true
},
lastName: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide lastName"],
    trim: true
},
businessName: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide your business name"],
    // unique: true,
    trim: true
},
businessLocation: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide your business location"],
    trim: true
},
IDNumber: {
    type: String,
    minlength: [5, "This field requires a minimum of 5 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide ID Number or a passport"],
    // unique: true,
    trim: true
},
telephone: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide telephone"],
    // unique: true,
    trim: true
},
industryCategory: {
    type: String,
    minlength: [3, "This field requires a minimum of 3 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please enter industry's category"],
    trim: true
},
email: {
    type: String,
    minlength: [5, "This field requires a minimum of 5 characters"],
    maxlength: [50, "This field requires a maximum of 50 characters"],
    required: [true, "Please provide an email"],
    match: [/^\w ([\.-]?\w )*@\w ([\.-]?\w )*(\.\w{2,3}) $/, 'Please provide a valid email address'],
    // unique: true,
    trim: true,
    lowercase: true
},
password: {
    type: String,
    minlength: [5, "This field requires a minimum of 5 characters"],
    maxlength: [1500, "This field requires a minimum of 1500 characters"],
    required: [true, "Please provide password"],
},
resetPasswordToken : String,
resetPasswordExpiry : Date
})

This is my controller related to the above schema

exports.vendorRegistration = async (req, res, next) => {
try {
    const {
        firstName, 
        lastName, 
        businessName, 
        businessLocation, 
        IDNumber, 
        industyCategory, 
        email, 
        telephone, 
        password 
    } = req.body

    const vendor = await Vendor.create({
        firstName, 
        lastName, 
        businessName, 
        businessLocation, 
        IDNumber, 
        industyCategory, 
        email, 
        telephone, 
        password 
    })

    sendVendorToken(vendor, 201, res)
    
} catch (error) {
    next(error)
}
}

The sendVendorToken is a function that takes in those arguements and returns a token. This is the error middleware for catching duplicated values

    if(err.code === 11000){
    const message = "Duplication Key Error"
    error = new ErrorResponse(message, 400)
}

When I try to send a post request to create the vendor i get the duplication key error. This is the result from postman enter image description here

I have no idea why I am getting this error

enter image description here

From the image above that field does not exist i have completely changed the that field name to industryType in my schemas and controllers but i am still getting index: industryCategory_1 dup key: { industryCategory: null }" error. Could the problem be postman or what? Should i re-install postman? I am confused

CodePudding user response:

I had the same problem in the past, in my case I resolved the problem by going to mongo compass and deleting users(in your case will be vendors) collection from the database.

In case you don't want to lose existing data in vendors' collection, you can query all vendor documents and store them somewhere else as JSON, delete vendors' collection and then insert back all vendors' documents

  • Related