Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
unique:true not reading in mongoose. Also Model.create not throwing error

I am trying to export an excel file to Mongodb. I have used convert excel to Json library. I have created the keys using mongoose.schema. I have kept the email as unique:true, but when the excel data is loaded into mongodb, the repeated email document is appearing in the database(which I do not want as i have kept email as unique:true).

I am sharing my node.js code.

'use strict';
const excelToJson = require('convert-excel-to-json');


// -> Read Excel File to Json Data



const excelData = excelToJson({
    sourceFile: 'Klimb Assignment - Add from Excel Data.xlsx',
    name: 'Participants',
    header:{
        // Is the number of rows that will be skipped and will not be present at our result object. Counting from top to bottom
        rows: 1 // 2, 3, 4, etc.
    },
    columnToKey: {
        '*': '{{columnHeader}}',
    }

});


const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
 await mongoose.connect('mongodb://localhost:27017/mydb');
}
  const excelSchema = new mongoose.Schema({
     'Name of the Candidate': { type : String, required : true},
     'Email' : { type : String, required : true, unique : true},
     'Mobile No.': Number,
     'Date of Birth': String,
     'Work Experience': String,
     'Resume Title': String,
     'Current Location': String,
     'Postal Address': String,
     'Current Employer': String,
     'Current Designation': String
  });
  
  
   const excel = mongoose.model('aspirants', excelSchema);
    excel.create(excelData.Sheet1, function(err){
    if (err) throw err;
  });
   

I have used excel.create and inside create I have passed the array that was retrieved from the JSON file.

Also, I would like to clarify that my code has not syntax error. There is some kind of bug that I am not able to find. I have installed all the required node.js files and all of them have latest versions.

Surprisingly, the code executed correctly for only 1 time. Afterwards it is not working correctly.

Your help is highly appreciated.

The excel file consists of a repeated entry(Email) and it should throw the error for repeated entry.

Can anyone find the bug.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!