how to add findone for name validation in nodejs details from mongodb

35 views Asked by At

anyone of you can slove this problem the duplication of name to avoid and execute on postman api the codes are attached below

*employeemodel.jsenter image description here

*employeecontroller.jsenter image description here

*routes.jsenter image description here

1

There are 1 answers

0
eekinci On

I'm not sure if I understood your question correctly. But if you would like to prevent double names in your EmployeeModel, you can use Unique Indexes like unique: true in your employeemodel.js:

const employeeSchema = new Schema({
  name: {
    type: String,
    required: true,
    unique: true
  }
})

On saving a new model with an existing name, in employeeController.js on await employeeModelData.save() you will get an error, this will be caught with your try-catch.

If you need further information and examples, you can take a look on: Understanding unique in Mongoose.