Home > OS >  Node.js REST API Will only allow me to create one record before throwing error?
Node.js REST API Will only allow me to create one record before throwing error?

Time:10-24

A bit of a back end issue here. I have a simple API i've built which is just a post endpoint. This should post data with the req.body. Now - weirdly, it works all fine, but when I try it again, I will recieve an error like this.

{
    "index": 0,
    "code": 11000,
    "keyPattern": {
        "centralEmail": 1
    },
    "keyValue": {
        "centralEmail": null
    }
}

Now this is weird because centralEmail is not apart of this model or router at all. There is no reference to it any point. When I POST the first time, it works fine - the second, this.

This is my schema:

const mongoose = require("mongoose")


//this just regulates the roles and their data connected to them. Will build out with more aux data support

// //RolesPositionSchema
const RolesListingSchema = new mongoose.Schema(
    {
    role:{type:String, required: true, default: ""},
    companyListing:{type:String, required: false, default: ""},
    companyID:{type:String, required: false, default: ""},
    dateListing:{type:String, required: false, default: ""},
    status: {type: String, required: false},

    finalFormat:{type:String, required: false, default: ""},

    listingScore:{type: Number},
    companyEmail:{type:String},

    selectedStyle:{type:String, required: false, default: ""},
    selectedLocation:{type:String, required: false, default:""},
    selectedIndustry:{type:String, required: false},

    optionalMinSalaryRange:{type:String, required: false, default: ""},
    optionalMaxSalaryRange:{type:String, required: false, default: ""},
    optionalQuals:{type:String, required: false, default: ""},

    selectedInterview1:{type:String, required: false, default: ""},
    selectedInterview2:{type:String, required: false, default: ""},
    selectedInterview3:{type:String, required: false, default: ""},

    optionalSkills:{type:[String], required: false, default: []},

    optionalPerks:{type:[String], required: false, default: []},

    optionalWFH:{type:String, required: false, default: ""},

    optionalQuals:{type:[String], required: false, default: []},

    //
    lookingFor:{type:[String], required: false, default: []},
    dailyResponsibilites:{type:[String], required: false, default: []},
    FAQS:{type:[String], required: false, default: []},

    //
    requiredFields:{type:[String], required: false, default: []},

    },
);

module.exports = mongoose.model("RoleListings", RolesListingSchema)

And also, this is my API. Very simple but please see:

//CREATE A ROLE DESC
//Implement new roles
router.post("/", async (req,res)=>{
// router.post("/", verifyTokenAndAdmin, async (req,res)=>{
    const newRole = new Role(req.body)

    try{
        const savedRole = await newRole.save();
        res.status(200).json(savedRole)
    }catch(err){
        res.status(500).json(err)
    }
})

CodePudding user response:

This error indicate that you have duplicated value for a unique field.

You are not have the centralEmail in the schema right now, but you maybe had it before. When you remove the field that had unique index from the schema, that will NOT automatically remove the index too. You would have to do it manually.

For example, you can open the collection in Compass, and navigate to the Indexes tab. There, see if there is an index for the centralEmail property, and then delete it.

  • Related