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

217
Vistas
Async write after select Pouchdb

I'm trying to check if an element exist before inserting it in my bdd. I have to do this in order to (in the future) modify this existing element.

I'm using PouchDb and PouchDb-find with Node 6.9.1.

Actually I'm doing this:

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);
  };

But the problem is : Due to the asynchronous version of my select query... if an element exists two times it appends that the second select occurred BEFORE the insertion of the first element, and I have this element two times in my database. I don't know how to deal with this one.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

SO.. I'v found a workaround ! Simply create a function that I call recursivly after writting into my database. Goodbye for loop.

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 Denunciar
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