【问题标题】:How to set value of data in the firebase real time data base?如何在firebase实时数据库中设置数据值?
【发布时间】:2020-01-07 00:03:23
【问题描述】:

我尝试刷新数据库中变量的值。但是函数.set() 需要两个参数,我不明白第一个是什么。

我尝试使用.push() 函数,但这会在我的数据库中创建一个新行。我不想要那个。我想要改变价值。

所以我知道我需要使用.set() 函数,但她需要两个参数项:Firebase 操作和数据

第一个是什么?

    this.afAuth.authState.subscribe(data => { // ajoute des tacoPoint 
          this.test = this.afDatabase.list(`TacoPoint/${data.uid}`).valueChanges();

          this.test.forEach(data => { // ajoute des tacoPoint 
            data.forEach(e => {


              e.Tacopoint = e.Tacopoint + 20;
              console.log(e);

            })
          })
        })

        this.afAuth.authState.subscribe(auth => {
         this.afDatabase.list(`TacoPoint/${auth.uid}`).set(); // need to set the new TacoPoint in the data base 

        })

【问题讨论】:

  • 你使用的是firebase还是firestore?
  • 我使用firebase(实时数据库)

标签: angular firebase-realtime-database angularfire2


【解决方案1】:

假设你想设置对象data

this.afDatabase.collection("TacoPoint").doc(auth.uid).set(data);

【讨论】:

  • 感谢您的快速回答,但 Angular Fire 模块上不存在 .collection ^^
  • 从 '@angular/fire/firestore' 导入 { AngularFirestore };从'firebase'导入*作为firebase;
  • 我在app模块和页面中导入但是不起作用可能需要安装?
  • 是的,你可以参考这个。它与 firebase 进行了 CRUD 操作。 github.com/AdritaS/Angular-Firebase/blob/master/src/app/…
【解决方案2】:

所以我成功使用了.set() 我们只需要像这样在数据库中的对象上做一个引用:

const a = this.afDatabase.object(`TacoPoint/${data.uid}`) // reference 

a.set(/*some data*/) // now you can use the set function 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2020-09-20
    • 2019-01-18
    • 1970-01-01
    • 2021-03-30
    • 2022-01-05
    相关资源
    最近更新 更多