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!' });
});
})