Javascript Ionic 4在internet连接上存储表单数据和同步

Javascript Ionic 4在internet连接上存储表单数据和同步,javascript,angular,cordova-plugins,ionic4,Javascript,Angular,Cordova Plugins,Ionic4,我有一个应用程序,我需要在离线模式下捕捉狗。我使用了formdata,因为我正在发送图像以及剩余的数据。我使用下面的代码来存储HTTP请求 storeRequest(url, type, data) { this.sharedService.presentToast('Your data is stored locally because you seem to be offline.'); let action: StoredRequest = { url:

我有一个应用程序,我需要在离线模式下捕捉狗。我使用了formdata,因为我正在发送图像以及剩余的数据。我使用下面的代码来存储HTTP请求

storeRequest(url, type, data) {
   this.sharedService.presentToast('Your data is stored locally because you 
   seem to be offline.');

   let action: StoredRequest = {
      url: url,
      type: type,
      data: data,
      time: new Date().getTime(),
      id: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5)
   };

   return this.storage.get(STORAGE_REQ_KEY).then(storedOperations => {
      let storedObj = JSON.parse(storedOperations);

      if (storedObj) {
          storedObj.push(action);
      } else {
          storedObj = [action];
      }

      return this.storage.set(STORAGE_REQ_KEY, JSON.stringify(storedObj));
    });
 }
我在这里存储所有请求。当我试图检索数据时,得到的是一个空对象。这就是我无法将数据同步到服务器的原因。我使用了下面的代码来同步请求

checkForEvents(): Observable<any> {
return from(this.storage.get(STORAGE_REQ_KEY)).pipe(
  switchMap((storedOperations: any) => {
    let storedObj = JSON.parse(storedOperations);
    console.log(storedObj);
    if (storedObj && storedObj.length > 0) {
      console.log(storedObj);
      return this.sendRequests(storedObj).pipe(
        finalize(() => {
          this.sharedService.presentToast('Local data succesfully synced to 
          API!');
          this.storage.remove(STORAGE_REQ_KEY);
        })
      );
    } else {
      console.log('no local events to sync');
      return of(false);
    }
  })
)
}
checkForEvents():可观察{
从(this.storage.get(storage\u REQ\u KEY))管道返回(
switchMap((storedOperations:any)=>{
让storedObj=JSON.parse(storedOperations);
控制台日志(storedObj);
if(storedObj&&storedObj.length>0){
控制台日志(storedObj);
返回此.sendRequests(storedObj).pipe(
完成(()=>{
this.sharedService.presentToast('本地数据已成功同步到
空气污染指数();;
此.storage.remove(存储请求键);
})
);
}否则{
log(“没有要同步的本地事件”);
归还(假);
}
})
)
}
**编辑-** 现在我的问题是什么是存储数据的良好实践??我们在哪里可以存储formdata???

您可以收听窗口并执行您的操作,例如

window.addEventListener('online',  ()=>{
//save your data
});
请检查一下