【问题标题】:Key being returned from database is different to the one shown从数据库返回的密钥与显示的不同
【发布时间】:2017-07-30 21:37:51
【问题描述】:

我正在尝试获取在将项目推送到数据库时由 Firebase 数据库生成的密钥,但是,返回的密钥具有不同的值。在照片中,您可以看到 'key' (-kqI0bxKg...) 的值与推送对象主键的值不同。为什么返回不同的密钥(-kql0bxKgq...)?如何使用 angularfire 和 typescript 解决这个问题?

  handler: data => {
    this.afAuth.authState.subscribe(auth => {
        const currentUser = auth.uid;
        var myRef = this.afDatabase.database.ref().push()
        var myKey = myRef.key;
        console.log(myKey);
        var newEntry = {
            description: data.description,
            number: 0,
            createdBy: currentUser,
            key: myKey
        }
        this.afDatabase.list('whatIWant').push(newEntry);
    })
  }

【问题讨论】:

    标签: typescript firebase-realtime-database angularfire


    【解决方案1】:

    每次调用push() 时都会生成一个新 ID。由于您在代码中的两个位置调用 push(),因此您生成了两个单独的 ID。要在两个地方使用相同的 ID,请调用一次 push() 并在两个地方使用新的引用:

        var myRef = this.afDatabase.database.ref().push()
        var myKey = myRef.key;
        ...
        myRef.set(newEntry);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2020-04-23
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多