Using Observables in Ionic 3 and Angular 5

#1

Hi, I’m new in ionic 3,

  1. I can save sqlite data save to server database by using API (http).
  2. I have 4 data in my sqlite local storage.
  3. Following function: for each data is saved to server database successfully…
    a) after success i got a response from server.
    b) And according success message i run another function for delete single data(delete each data)
    c) But what I am seeing only no. 4 data is delete other remain same.

Here is my code:

synch(){

this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“SELECT * FROM m_offline_pod_data ORDER BY m_offline_id ASC”, ).then((data) => {
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {

    this.productInfo.m_offline_id = data.rows.item(i).m_offline_id;
    this.productInfo.m_pod_no = data.rows.item(i).m_pod_no;

this.http.post(“http://example.in/clix_mobile_apps/add-product-offline.php”,this.productInfo).subscribe( data => {
console.log( "SUCCESS => " + data);

let offlineupdate = JSON.parse(data["_body"]);

  if(offlineupdate.status == "success"){

this.MYdeleteData(this.productInfo.m_offline_id);

  }  else if(offlineupdate.status == "podnotmatch"){

 this.toast.show("POD MISMATCH...try again...", '5000', 'center').subscribe(
      toast => {
      console.log(toast);
      }
    );

  }

}, err=>{
  console.log("Error => "+ err);

    this.toast.show("NO SERVER RESPONSE...try again...", '5000', 'center').subscribe(
      toast => {
      console.log(toast);
      }
    );

});
}  // for each end
                       }
    console.log(this.products);
}, (error) => {
    console.log("ERROR: " + JSON.stringify(error));
             });
})

}

// delete function

MYdeleteData(myID){
alert(‘DELETE’+myID);
this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“DELETE FROM m_offline_pod_data WHERE m_offline_id = '”+myID+"’", ).then((data) => {

 console.log(this.products);

}, (error) => {
  //alert('delete error v3-10012')
    console.log("ERROR: " + JSON.stringify(error));
  });

})
}

Please help me

#2

It’s not clear what your code is… quite unreadeable since unformated.
Perhaps you call your delete function outside of the result of the subscribe ?

I would debug here

#3

Yes the delete function outside from result but function call with in response… I am attach a pic where i try to show the scenario …

My problem is that:

  1. there is 4 row
  2. for every row getting response.
  3. after getting response the delete function work only last row
  4. where i expect every time delete function work and remove the data from local storage.
#4

Yes the delete function outside from result but function call with in response… I am attach a pic where i try to show the scenario …

My problem is that:

there is 4 row
for every row getting response.
after getting response the delete function work only last row
where i expect every time delete function work and remove the data from local storage.

Please help me :cry::cry::cry:

#5

still can’t realise what’s going on…

I would do something like :

post_stuff(i).subscribe{res=>{
     this.delete(res.i); // or something linking to the data you want to delete
}
#6

This is what i am want:#####

Example, if I had 3 local records then I would want the code to execute things in the following order:

read data from local storage and enter for each loop:
…for first each #1
Upload data item #1 to online server (http)

  • [Wait for upload to complete]
    Delete data item #1 from local storage
  • [Wait for delete complete]

…for each #2
Upload data item #2 to online (http)

  • [Wait for upload to complete]
    Delete data item #2 from local storage
  • [Wait for delete complete]

…for each #3
Upload data item #3 to online (http)

  • [Wait for upload to complete]
    Delete data item #3 from local storage
  • [Wait for delete complete]

synch(){

this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“SELECT * FROM m_offline_pod_data ORDER BY m_offline_id ASC”, ).then((data) => {
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {

this.productInfo.m_offline_id = data.rows.item(i).m_offline_id;
this.productInfo.m_pod_no = data.rows.item(i).m_pod_no;

this.http.post(“http://example.in/clix_mobile_apps/add-product-offline.php”,this.productInfo).subscribe( data => {
console.log( "SUCCESS => " + data);

let offlineupdate = JSON.parse(data["_body"]);

if(offlineupdate.status == “success”){
this.MYdeleteData(this.productInfo.m_offline_id);

} else if(offlineupdate.status == “podnotmatch”){

this.toast.show(“POD MISMATCH…try again…”, ‘5000’, ‘center’).subscribe(
toast => {
console.log(toast);
}
);

}

}, err=>{
console.log("Error => "+ err);

this.toast.show("NO SERVER RESPONSE...try again...", '5000', 'center').subscribe(
  toast => {
  console.log(toast);
  }
);

});
} // for each end
}
console.log(this.products);
}, (error) => {
console.log("ERROR: " + JSON.stringify(error));
});
})
}

// delete function

MYdeleteData(myID){
alert(‘DELETE’+myID);
this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“DELETE FROM m_offline_pod_data WHERE m_offline_id = '”+myID+"’", ).then((data) => {

console.log(this.products);

}, (error) => {
//alert(‘delete error v3-10012’)
console.log("ERROR: " + JSON.stringify(error));
});
})
}

#7

Thank you @ trollanfer

This is what i am want:#####

Example, if I had 3 local records then I would want the code to execute things in the following order:

read data from local storage and enter for each loop:
…for first each #1
Upload data item #1 to online server (http)

  • [Wait for upload to complete]
    Delete data item #1 from local storage
  • [Wait for delete complete]

…for each #2
Upload data item #2 to online (http)

  • [Wait for upload to complete]
    Delete data item #2 from local storage
  • [Wait for delete complete]

…for each #3
Upload data item #3 to online (http)

  • [Wait for upload to complete]
    Delete data item #3 from local storage
  • [Wait for delete complete]

this is not happening by this code:

synch(){

this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“SELECT * FROM m_offline_pod_data ORDER BY m_offline_id ASC”, ).then((data) => {
if(data.rows.length > 0) {
for(var i = 0; i < data.rows.length; i++) {

this.productInfo.m_offline_id = data.rows.item(i).m_offline_id;
this.productInfo.m_pod_no = data.rows.item(i).m_pod_no;

this.http.post(“http://example.in/clix_mobile_apps/add-product-offline.php”,this.productInfo).subscribe( data => {
console.log( "SUCCESS => " + data);

let offlineupdate = JSON.parse(data["_body"]);

if(offlineupdate.status == “success”){
this.MYdeleteData(this.productInfo.m_offline_id);

} else if(offlineupdate.status == “podnotmatch”){

this.toast.show(“POD MISMATCH…try again…”, ‘5000’, ‘center’).subscribe(
toast => {
console.log(toast);
}
);

}

}, err=>{
console.log("Error => "+ err);

this.toast.show("NO SERVER RESPONSE...try again...", '5000', 'center').subscribe(
  toast => {
  console.log(toast);
  }
);

});
} // for each end
}
console.log(this.products);
}, (error) => {
console.log("ERROR: " + JSON.stringify(error));
});
})
}

// delete function

MYdeleteData(myID){
alert(‘DELETE’+myID);
this.sqlite.create ({ name: “m_ilsapps.db”, location: “default” })
.then((db: SQLiteObject) => {
db.executeSql(“DELETE FROM m_offline_pod_data WHERE m_offline_id = '”+myID+"’", ).then((data) => {

console.log(this.products);

}, (error) => {
//alert(‘delete error v3-10012’)
console.log("ERROR: " + JSON.stringify(error));
});
})
}