2

This is my equipment schema inside my model-

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var equipmentSchema = new mongoose.Schema({
    name         :String,
    amount       :Number,
    category     :String,

});

//adds some methods from the 'PLM' package to our equipmentSchema
equipmentSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("Equipment", equipmentSchema);

To post data into the DB;

var Equipment  = require("./models/equipment")

//handling equipment details
app.post("/Dashboard/equipments", function(req,res){
    Equipment.create(new Equipment({name:req.body.name, amount:req.body.amount,  category: req.body.category}),function(err, equipment){
        if(err){
            console.log(err);
            return res.render("Dashboard/equipments");
        }
    });

});

I get this error

message: 'E11000 duplicate key error collection: Fitness.equipment index: username_1 dup key: { : null }', driver: true, index: 0,
code: 11000,

In my table, there's no 'Username' field and no duplicate records too.I can save only one record to the DB and if i try again, i get the duplicate error above. What i'm i doing wrong?

Sala
  • 43
  • 10
  • Are you absolutely sure that there are no duplicate records? Make sure that you are using the correct DB and checking the right table. – Nikhil Baliga Jul 02 '18 at 07:11
  • @NikhilBaliga Yes, i double checked it and it's still the same. I'm using the correct DB and tables :) – Sala Jul 02 '18 at 07:19
  • Possible duplicate of [E11000 duplicate key error index in mongodb mongoose](https://stackoverflow.com/questions/24430220/e11000-duplicate-key-error-index-in-mongodb-mongoose) – Ratan Uday Kumar Jul 02 '18 at 07:27
  • go to your robomongo or whatever GUI you are using and remove the username index manually... or try this `Model.dropIndex({ username:1 })` – Ashh Jul 02 '18 at 07:51
  • by the way why are you using PLM package on equipment. As per the PLM docs. the default usernameField is username and it is set by default unique. In your case the first record creates username: null, and second record does the same. thus you get duplicate error. – Farhan Tahir Jul 02 '18 at 08:11
  • In documentation of PLM: usernameField: specifies the field name that holds the username. Defaults to 'username'. This option can be used if you want to use a different field to hold the username for example "email". usernameUnique : specifies if the username field should be enforced to be unique by a mongodb index or not. Defaults to true. – Farhan Tahir Jul 02 '18 at 08:11
  • There is a work around set usernameField to name and make unique to false. but again why are you using plm here ? – Farhan Tahir Jul 02 '18 at 08:11
  • 1
    @FarhanTahir first, I manually remove the unwanted index with 'db.equipment.dropIndex('username_1')' and again I included the PLM by mistake, was an error. Now it works. – Sala Jul 02 '18 at 09:05

0 Answers0