Search code examples
node.jsmongodbpostman

Can't post with postman, why?


employee.js

const mongoose = require('mongoose');

var Employee = mongoose.model('Employee', {
  name: { type: String },
  position: { type: String },
  office: { type: String },
  salary: { type: Number }
});

module.exports = { Employee };

employeeController.js

const express = require('express');

var router = express.Router();

var { Employee } = require('../models/employee');

router.get('/', (req, res) => {
Employee.find((err, docs) =>{
    if (!err) {res.send(docs);}
    else {console.log('Error in Retriving Employees: '+ JSON.stringify(err, undefined, 2));}
   });
});

router.post('/', (req, res) => {
var emp = new Employee({
    name: req.body.name,
    position: req.body.position,
    office: req.body.office,
    salary: req.body.salary,
});
    emp.save((err, doc) => {
    if(!err) {res.send(doc); }
    else {console.log('Error in employee save:' + JSON.stringify(err, undefined, 
   2));}
 });
});

 module.exports = router;

In postman I add:

  {
"name":"Anida Mujezin",
"position":"Full stack developer",
"office":"Sarajevo",
"salary":3000
  }

Error: Error in employee save:{ "driver": true, "name": "MongoError", "index": 0, "code": 13297 }

and Could not get response in Postman


Solution

  • Try changing the name of your database. That error code seems to indicate that a database with that name already has been created, and the name needs to be unique.

    However, as the error message suggests, there is also a possibility that you have already created an employee with the name "Anida Mujezin", which I assume is used as the identifier in your database.