I am trying to create multiple objects with mongodb, mongoose and express (by using Insomnia). I have managed to create the first object, but when I try to create the following one it gives me the following error:
{
"success": false,
"message": {
"driver": true,
"name": "MongoError",
"index": 0,
"code": 11000
}
}
I tried to solve it by including different data in the object (although I didn't specify uniqueness in the properties), but it will throw the 11000 (duplicate key) error anyways.
The log of req.body returns { inmovilizadoInmaterial: 12, inmovilizadoMaterial: 13 } (as the data included for the registration are those values)
Here's the model
const mongoose = require("mongoose");
const { Schema } = mongoose;
const balanceSchema = new Schema(
{
inmovilizadoInmaterial: { type: Number, default: 0 },
inmovilizadoMaterial: { type: Number, default: 0 },
}
);
module.exports = mongoose.model("Balance", balanceSchema);
Here's the router that connects the model and the controller the object:
const express = require('express');
const router = express.Router();
const balanceController = require('../controllers/balance.controller');
router.post('/create', balanceController.create);
module.exports = router;
And finally here's the controller that has the function that creates the object.
const balanceController = {};
const Balance = require('../models/Balance')
balanceController.create = async(req,res)=>{
const balance = new Balance(req.body);
balance.save();
}
module.exports = balanceController;
I know it must be a very simple mistake but I'm very new to the technology.
Thank you very much in advance!