angularfire2,仅将数据推送/更新到特定的子/密钥

时间:2018-01-08 08:29:57

标签: angular typescript firebase firebase-realtime-database angularfire

我有一个推送数据的功能,另外两个属性必须被推送/更新/存储到同一个键。有percentMalepercentFemale。我需要将这两个属性的提交结果放在同一个子/键中。我们说密钥是L2Jq0Tg4JOS66F786pT

male: number = 0;
female: number = 0;
percentMale;
percentFemale;
insertdata(data: App) {
  if (data.data1 == 'Male') {
    this.male++;
  } else if (data.data1 == 'Female') {
    this.female++;
  }
  this.percentMale = this.male / (this.male + this.female) * 100;
  console.log('percentMale', this.percentMale + '%');

  this.percentFemale = this.female / (this.male + this.female) * 100;
  console.log('percentFemale', this.percentFemale + '%');

  this.dataList.push({
    data1: data.data1,
    data2: data.data2,
    data3: data.data3,
    time: new Date().getTime(),
  });

  this.dataList.push({
    percentMale: this.percentMale,
    percentFemale: this.percentFemale,
  });

  //if a key already exist
  //this.dataList.update(data.$key, {
  //percentMale: this.percentMale,
  //percentFemale: this.percentFemale,
  //});
}

我所做的功能导致更多percentMalepercentFemale分开存储,表格显示不必要的行。

enter image description here

有人能帮我解决这个问题吗?如果需要更多的片段,请告诉我。

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。



this.pertanyaanList.update("databaseKey", {
      percentMale: this.percentMale,
      percentFemale: this.percentFemale,
      });