Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda