【问题标题】:AngularFire get and set a single value from/to a firebase databaseAngularFire 从/向 Firebase 数据库获取和设置单个值
【发布时间】:2018-08-29 19:50:12
【问题描述】:

我的 firebase 数据库称为 verhuurfietsen,我想要获取和设置的最大数量为 4,但最大值仍必须为 4。

 obj;
 constructor(private db: AngularFireDatabase){}
 ngOnInit() {
  this.obj = this.db.database.ref('amount');
 }

 updateMaxValue(){
  this.obj.update(this.max)
 }

推送不是问题,但我似乎无法在任何地方找到如何从 firebase 数据库中获取单个值。我想用 firebase db 中的值初始化我的 max 变量

【问题讨论】:

  • 你的意思是“仍然最大值必须是 4”。当您更新该值时肯定会发生变化。
  • 好吧,最大数量是 4,所以在我的代码中,我将使它永远不会高于 4,但我仍然想获得这个值,我只看到列表和对象,但不是一个简单的方式

标签: typescript firebase firebase-realtime-database angularfire2


【解决方案1】:

以Angularfire2方式尝试

ngOnInit() {
  this.db.object('amount').valueChanges().subscribe(action => {
      console.log(action);
      for(var obj in action) { 
          console.log(obj); 
          this.max = Number.parseInt(obj);
      }
    });
}

【讨论】:

  • 我试试看。 :)
  • TS2399:类型“{}”上不存在属性“val”。 val() 不存在?
  • 当你做console.log(action)时你会得到什么
  • a 4 但它是 {} 而不是数字 有没有办法解析或转换它?
  • 这工作:this.db.object('amount').valueChanges().subscribe(action => { console.log(action); for(let obj in action) { console.log(obj); this.max = Number.parseInt(obj); } });
猜你喜欢
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
相关资源
最近更新 更多