Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda