如何将数据从Angular表单推入(数组)队列

时间:2018-10-22 15:42:14

标签: javascript angular

我想将来自Ionic应用程序的表单提交本地存储到某种队列中以脱机存储,并允许用户稍后提交以从队列中弹出表单数据进行提交。我在完成该任务时遇到问题:

表格数据:

0: Array(1)
0:Location: {ID: "6", Name: "Alto Minerals-Cowden D-27", Lat: "35.8836757", Lon: "-108.3292188", Batch_Treatments: Array(2)}
date: "10-22-2018"
doubleSlug: "No"
notTreated: ""
updatedLocation: {ID: "6", Name: "Alto Minerals-Cowden D-27", Lat: "35.8836757", Lon: "-108.3292188", Batch_Treatments: Array(2)}
1: {gpsLoc: "35.884802254350003,-110.54321959999999"}
2: {md5: "fc9887f481af2a68fece9ca4815796b8"}
3: {appVersion: undefined}

当我尝试将其推入数组时,出现错误,

  

类型:“ exception”,消息:“无法读取null的属性'push'”,名称:“ TypeError”

我不确定自己在做什么错,我正在像这样初始化数据:

 this.summaryDetails = form.value;
 this.md5Data = md5.appendStr(JSON.stringify(this.summaryDetails)).appendStr(this.lonlat.toString()).appendStr(time.getTime().toString()).end();
 this.submitAllData.push(this.summaryDetails,{'gpsLoc':this.lonlat.toString()},{'md5':this.md5Data},{'appVersion':this.reap.omegaVersion});
 public offlineFormSubmissions:any [] = [];//from service  
 this.offlineFormSubmissions.push(this.submitAllData);//where I am getting error  

1 个答案:

答案 0 :(得分:0)

所以我找到了我的问题,不幸的是,这完全是我的错。应用加载时,我的服务将本地存储中的所有变量本地化,如果本地存储为空,则会为offlineFormSubmissions变量赋予 null 值。

因此,显然如果它为null,则无法向其推送任何内容,因此也为什么会出现错误。我创建了if语句来处理变量,现在一切正常!