1

Have a model

const mongoose = require("mongoose");
const botSchema = new mongoose.Schema({
  code: {
    type: String,
    required: true,
    unique: true,
  },
  description: {
    type: String,
    required: true,
  },
  botStatus: {
    type: String,
    required: true,
  },
});
// botSchema.plugin(require("mongoose-beautiful-unique-validation"));
module.exports = mongoose.model("Bot", botSchema);

route ===== // Creating one

router.post("/", async (req, res) => {
  const bot = new Bot({
    code: req.body.code,
    description: req.body.description,
    botStatus: req.body.botStatus,
  });
  try {
    const newBot = await bot.save();
    res.status(201).json(newBot);
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
});

==== when post using postman or Rest Client from VS Code

POST http://localhost:4000/bots
Content-Type: application/json

{
  "code": "code 04",
  "description": "desc 04",
  "botStatus": "status 04"
}

it gives me the error

{
  "message": "E11000 duplicate key error collection: botmanager.bots index: route_id_1 dup key: { route_id: null }"
}

I have no ideas where is route_id_1 dup key is coming from

Vavan
  • 21
  • 3

0 Answers0