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

228
Views
Escritura asíncrona después de seleccionar Pouchdb

Estoy tratando de verificar si existe un elemento antes de insertarlo en mi bdd. Tengo que hacer esto para (en el futuro) modificar este elemento existente.

Estoy usando PouchDb y PouchDb-find con Node 6.9.1.

En realidad estoy haciendo esto:

 for(var i = 0; i < 10;i++ ){ (function(_count, _pdb){ var count = _count; var db = _pdb; db.find({ selector: {numeroCandidat: parseInt(results[count].no_apb)} }).then((result) => { if(result.docs.length != 0){ console.log("l'étudiant existe"); }else{ console.log("l'étudiant n'existe pas"); var etudiant = { "numeroCandidat": results[count].no_apb, "nom": results[count].nom, "numeroGroupe": "gr" + results[count].groupe, "filiere": results[count].libelle, }; db.post(etudiant).then((response) =>{ // handle response console.log("STUDENT CREATED"); }).catch(function (err) { console.log(err); }); } }).catch(function (err) { }); })(i, this.pdb); };

Pero el problema es: debido a la versión asíncrona de mi consulta de selección... si un elemento existe dos veces, agrega que la segunda selección ocurrió ANTES de la inserción del primer elemento, y tengo este elemento dos veces en mi base de datos. No sé cómo lidiar con este.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

SO... ¡He encontrado una solución! Simplemente cree una función a la que llame recursivamente después de escribir en mi base de datos. Adiós al bucle.

 var createStudents = function(_count, _pdb, _students){ if(_count >= 10) return; console.log(_count); var count = _count; var db = _pdb; var students = _students.slice(0); db.find({ selector: {numeroCandidat: parseInt(students[count].no_apb)} }).then((result) => { if(result.docs.length != 0){ console.log("l'étudiant existe"); createStudents(++count,db,results); }else{ var etudiant = { "numeroCandidat": students[count].no_apb, "nom": students[count].nom, "numeroGroupe": "gr" + students[count].groupe, "filiere": students[count].libelle, "etudiantComms": [ {"commentaire": students[count].commentaire} ] }; db.post(etudiant).then((response) =>{ // handle response console.log("STUDENT CREATED"); createStudents(++count,db,results); }).catch(function (err) { console.log(err); }); } }).catch(function (err) { }); } createStudents(0,this.pdb,results);
over 4 years ago · Santiago Trujillo Report
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!