【问题标题】:How To Store Firebase Users Under UID如何在 UID 下存储 Firebase 用户
【发布时间】:2017-10-11 05:47:29
【问题描述】:

我一直在使用 push() 方法将所有用户存储在我的 firebase 数据库中。此方法为每个用户生成一个唯一的 id 并将它们存储在此 id 下。我正在尝试更改此设置,以便我的用户存储在使用 set() 方法时自动生成的 user.uid 下,但是,我无法将它们存储在此用户 uid 下。

我正在使用以下内容将用户添加到数据库中,我在这里缺少什么?

firebase.database().ref().child('accounts').set({
                email: user.email,
                userId: firebase.auth().currentUser.uid

              })

这使用 set() 并像这样存储数据:

{
  "accounts" : {
      "email" : "",
      "userId" : ""
    },
{
      "email" : "",
      "userId" : ""
    }
}

使用 push() 的结构如下所示: 用户是根据 firebase 生成的 push Id 存储的。

{
  "accounts" : {
    "-KRPSyO4B48IpBnHBTYg" : {
      "email" : "",
      "userId" : ""
    },
    "-KR4f4s5yvdr67g687nrth" : {
      "email" : "",
      "userId" : ""
    }
  }

我正在寻找的是这种结构: 用户由 firebase 生成的 userId 存储。

{
      "accounts" : {
        "userId value" : {
          "email" : "",
          "userId" : ""
        },
        "userId value" : {
          "email" : "",
          "userId" : ""
        }
      }

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    您尚未在路径中为 uid 本身创建节点。将其更改为:

    var uid = firebase.auth().currentUser.uid;
    firebase.database().ref().child('accounts').child(uid).set({
      email: user.email,
      userId: uid
    })
    

    【讨论】:

    • 这就是我所需要的。谢谢坦率!
    猜你喜欢
    • 2017-08-28
    • 2017-12-11
    • 2023-03-09
    • 2018-11-15
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多