1

I am trying to save entries to a MongoDB database using Node and Mongoose. I have a basic schema with two key/values. When I use the code below, I'm only able to add one entry to the database. On the second attempt, I get an error. E.g., I can add "John Doe" and "Male" to the database. But if I then try to add "Jane Doe" and "Female," I get an error.

I am getting the following error:

"success": false,

"message": "A project with that name already exists. "

Any thoughts on what I'm doing wrong?

var ProjectSchema = new Schema({
    name: {type: String, required: true},
    description: String
});

module.exports = mongoose.model('Project', ProjectSchema);

apiRouter.route('/projects')

    .post(function(req, res) {

        var project = new Project();        
        project.name = req.body.name;  
        project.description = req.body.description;  
        project.save(function(err) {
            if (err) {
              console.log(err);
                if (err.code == 11000)
                    return res.json({ success: false, message: 'A project with that exact name already exists. '});
                else
                     return res.send(err);
           }
            res.json({ message: 'Project created!' });
        });
    })
Community
  • 1
  • 1
Ken
  • 3,091
  • 12
  • 42
  • 69
  • @Salvador Dali I added the error to my post above. If I run db.projects.find(); in mongoshell, I get `{ "_id" : ObjectId("5518fa62860cf17245a43b26"), "description" : "Male", "name" : "John Doe", "__v" : 0 }` but nothing when I attempt to add more entries. Is that what you meant by what I see in my mongoshell? – Ken Mar 30 '15 at 07:29
  • yes, this is exactly what I wanted. And this is kind of strange because your mongoose schema only requires the name, does not tell that it is unique. Can you try to insert name: John1, description: male? And can you try to remove required after it and try it as well? Post your findings here. – Salvador Dali Mar 30 '15 at 07:35
  • @Salvador Dali I tried that but got the same error. And that second entry isn't added to the database. For what it's worth, I'm using Postman to make the entries just to be sure I wasn't doing something wrong with my code on the client side. – Ken Mar 30 '15 at 07:37
  • So basically you can only insert one person and then no matter what you do you can not insert any other? I mean you tried with different names and description and still failed? Can you output the whole `err` variable? Something like `if err(){ console.log(err)}` – Salvador Dali Mar 30 '15 at 07:39
  • @Salvador Dali Yes, exactly. I've tried with different names and descriptions. I even removed descriptions altogether from the schema but still ran into problems. – Ken Mar 30 '15 at 07:40
  • You need to give us the output of err, otherwise it could be anything. `console.log(err)` – rjmacarthy Mar 30 '15 at 07:46
  • @Richard Macarthy I'm not sure if I added console.log in right place above, but this the error I got: `{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: payments.projects.$username_1 dup key: { : null }] name: 'MongoError', code: 11000, err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: payments.projects.$username_1 dup key: { : null }' }` – Ken Mar 30 '15 at 07:54
  • Possible duplicate of [MongoDB E11000 duplicate key error](https://stackoverflow.com/questions/39988941/mongodb-e11000-duplicate-key-error) – YakovL Mar 09 '18 at 09:19

0 Answers0