【问题标题】:how can i get single value from firebase realtime database for ionic如何从 ionic 的 firebase 实时数据库中获取单个值
【发布时间】:2020-06-19 23:00:13
【问题描述】:

async incrementTuts(key) {
 let newData= {'tutsApplied':this.tutsApplied+1} 
 let newInfo = firebase.database().ref('thisis/'+this.route.snapshot.paramMap.get('key')).update(newData);
}

我想使用 ionic 4 在 firebase 实时数据库上增加 tutsApplied。我该如何实现。

更正的图像:

更新:

我用过

let key = this.route.snapshot.paramMap.get('key');
let ref = firebase.database().ref(`thisis/${key}/tutsApplied`);
ref.transaction((current) => {
  return (current || 0) + 1;
});

但新字段是在未定义下创建的,如图所示。我不想创建新领域。以及如何在控制台中打印当前值?

【问题讨论】:

  • 你现在得到了什么?
  • 好吧,当 Firebase 中的实际子名称为 tusApplied 时,该代码将无法工作,因为它试图访问 tutsApplied

标签: javascript firebase ionic-framework firebase-realtime-database


【解决方案1】:

听起来您希望根据属性的当前值为其写入新值。为此,您需要use a transaction

类似:

let key = this.route.snapshot.paramMap.get('key');
let ref = firebase.database().ref(`thisis/${key}/tutsApplied`);
ref.transaction((current) => {
  return (current || 0) + 1;
});

【讨论】:

  • 有问题的错字;孩子是 tusApplied
  • 很好的杰伊。如果这确实是问题的原因,那么问题可能应该作为一个错字关闭。
猜你喜欢
  • 2020-12-01
  • 2021-04-03
  • 2021-11-04
  • 2019-10-22
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多