0

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!

  • 1
    If you didn't specifically set any uniqueness constrains on any index then the only default constraint is on the `_id` field. i'm assuming you're sending it in the `req.body`. – Tom Slabbaert Sep 14 '21 at 13:12
  • Yes, it is included in the req.body. The problem, was that the duplicate key was userId (in an older version of the db it was a property that is not included in here anymore). I didn't know that the data in the db remained after changing the schema, I thought it would be updated when executing the server. Thank you tho! –  Sep 14 '21 at 13:29

1 Answers1

0

Can you try to change your controller like this:

balanceController.create = async(req,res)=>{
  try {
    console.log('Req body:', req.body);
    const balance = await Balance.create(req.body);
    res.status(200).json({success: true});  
  } catch(error) {
    console.log('ERROR: ', error);
    res.status(400).json({success: false, error});  
  }
}

Note that req.body will be logged so you can check whether your controller receiving correct data (maybe you are sending empty req.body from the frontend). Also error will be logged in. If this does not work, add both output of console.log(req.body) and console.log(error) to your question.

NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • Nope, same thing. As there's an error (11000) it will always enter the catch of your code. The problem is the second time I try to register information into the db. Literally the collection has 1 item. If I delete it, it will register the new one, but again, only allows one item in the db. –  Sep 14 '21 at 12:59
  • 1
    Yes, but try to `console.log(error)`. It will tell you want `key` is duplicated. Also, please add `console.log(req.body)` to your question. – NeNaD Sep 14 '21 at 13:06
  • 1
    The console.log(error) did the trick! The duplicate key was userId (in an older version of the db it was a property that is not included in here anymore). I didn't know that the data in the db remained after changing the schema, I thought it would be updated when executing the server. Thank you very much! –  Sep 14 '21 at 13:27
  • 1
    Noup, all data from before remains. It's just that new data will be created based on updated Schema Model. – NeNaD Sep 14 '21 at 13:30