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 Node 6.9.1 (please don't judge me)
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 extis two tims it appends that the second select occured BEFORE the insertion of the first element.. and I have this element two times in my database.... don't know how to deal with this one guys..
Thanks a lot for you help.
via Stephane Karagulmez
No comments:
Post a Comment