0

I have a component on my app where the user need to check if he is competent to work on a family of product. To do that, when the user is on his profile, he need to check some checkboxes, each checkbox representing one family.

screen app

When the user submit the form, I first delete all the lines representing his skills doing something like : DELETE FROM competent WHERE user = 7;

And after, I insert one by one his skills.

But, I don't know why, sometimes, it insert lines before deleting all the lines, and it ends with a bug.

Does anybody know how can I do to avoid this problem ?

This is the method I call when I submit my form :

onUpdateQualif(value: any) {
    //I delete all the lines here
      this.serviceQualif.deleteByOperateur(this.idOperateur).subscribe(data => data);
      var val = JSON.stringify(value);
      var values = val.split(",");
      var i = 1;
      for(let v of values){
        var debut = v.indexOf(":");
        var fin = v.indexOf("e");
        v = v.substring(debut+1, fin+1);
        if(v === "true"){
          var data = "{ \"operateur\" : " + this.idOperateur + " , \"famille\" : " + i+" }";
          var obj = JSON.parse(data);
//I insert the others one by one here.
this.serviceQualif.saveResource(this.serviceQualif.host+"/qualification", obj).subscribe(data => data);
        }
        i+=1
      }
1

You can subscribe to your http request and wait for it to complete before firing the saveResource function. The problem is caused because you are sending of two async functions, so it is very possible delete hasn't finished before save starts. You should subscribe to the deleteService and wait for it to complete before sending the saveService. An example below:


    onUpdateQualif(value: any) {
        //I delete all the lines here
          this.serviceQualif.deleteByOperateur(this.idOperateur).subscribe(
        data => {//Do something with data},
        error => {
        //Error handle
        },
        () => {
           //this will fire once the deleteByOperateur has completed. So call your saveService in here and you won't run into problems.

          var val = JSON.stringify(value);
          var values = val.split(",");
          var i = 1;
          for(let v of values){
            var debut = v.indexOf(":");
            var fin = v.indexOf("e");
            v = v.substring(debut+1, fin+1);
            if(v === "true"){
              var data = "{ \"operateur\" : " + this.idOperateur + " , \"famille\" : " + i+" }";
              var obj = JSON.parse(data);
    //I insert the others one by one here.
    this.serviceQualif.saveResource(this.serviceQualif.host+"/qualification", obj).subscribe(data => data);
            }
            i+=1
        }
        );      
          }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.