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

216
Views
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 answers
Answer question

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