【问题标题】:Why is the collection in Typescript update twice in firebase?为什么 Typescript 中的集合在 Firebase 中更新两次?
【发布时间】:2020-08-08 01:00:46
【问题描述】:

 // round page
  newRound() {
    console.log('new round ', this.round);
    this.you.score = +this.score;
    this.round++;
    console.log('THE PLAYERS -> ', this.players);
    this.afStore.collection('matches').doc(this.gameRoom).collection('match').doc(`${this.round}`).set({players: this.players, round: this.round}).then(() => {
      console.log('UPDATED GAME');
    });
    this.router.navigate(['/events/tab2/' + this.gameRoom]);
  }
 // Tab page that goes to the round page
// NOTE: this is for the next page, round 1,2,3,4 etc...
  theRound() {
    this.newRound++;
    console.log('this is the new round ', this.newRound);
    this.afStore.collection('matches').doc(this.gameRoom).collection('match').doc(`${this.newRound}`).set({players: this.players, round: this.newRound});
    this.afStore.collection('matches').doc(this.gameRoom).collection('match').doc(`${this.newRound}`).valueChanges().subscribe((v: any) => {
      this.router.navigate(['/events/tab2/' + this.gameRoom + '/' + v.round]);
      console.log('data returned from firebase ', v);
    });
  }


大家好!我有问题。我正在尝试更新firebase中的特定集合,但它会创建集合,但一旦我在圆形页面中按下一个值,它就不会更新“分数”。它还使用更新的分数创建第二个集合。任何帮助将不胜感激。

【问题讨论】:

  • 你在哪里更新分数
  • 我正在通过离子输入更新它,其中 NgModel 是得分( this.score )。

标签: javascript typescript firebase ionic4


【解决方案1】:

好吧,你用set() 更新你的文档,firebase 文档说:

如果文档不存在,则会创建它。如果文档确实存在,则其内容将被新提供的数据覆盖,除非您指定数据应合并到现有文档中

但在更新之前,您对 this.newRound 进行增量,因此您的文档是一个更高的增量,而 firebase 会创建一个新的。

【讨论】:

  • 我有点明白你在说什么。您有什么建议以便它只更新单个文档而不创建新文档?
  • 我不是最好的数据模型设计师,尤其是在基于文档的数据库中,这也取决于您要达到的目标。如果您发布您的数据库架构,我可以看看。
  • 但是@Peter Haddad 也是对的。您的数据库查询中的分数在哪里?
  • 分数包含在this.player 数组中。如何发布我的数据库架构?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2020-08-23
  • 1970-01-01
  • 2020-03-19
相关资源
最近更新 更多