【问题标题】:Can not store a new record in firebase realtime database无法在 Firebase 实时数据库中存储新记录
【发布时间】:2019-04-10 10:55:15
【问题描述】:

我有这段代码:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(function(data){
      firebase.database().ref('Users').child('006').set({
        email: data.user.email,
        createdAt: data.user.createdAt
      }).then((data)=>{
        //success callback

        console.log('data ' , data);
      }).catch((error)=>{
        //error callback
        console.log('error ' , error)
      })
    }).catch(function(error) {
      //Handle error
    });

这很简单,它的基本作用是对 Firebase 系统进行身份验证,一旦完成,就将新记录存储在我在那里配置的实时数据库中。

问题在于这部分代码:

email: data.user.email,
createdAt: data.user.createdAt

如果我这样离开它,它会使用“数据”值(在这个阶段确实可用)并且不会在 firebase 中创建新记录。如果我这样做:

email: 'data.user.email',
createdAt: 'data.user.createdAt'

立即添加一条新记录(当然,不是使用 email 和 createdAt 的实际值,而只是使用字符串)。我曾尝试 JSON.stringify/.toString() 他们但再次没有成功。我真的不知道我需要做什么才能将实际值添加到传递给 set 方法的 JSON。

还有一个缺点是我收到了this

返回的格式和示例内容

firebase.auth().createUserWithEmailAndPassword(email, password)

在“then”参数列表中是:

   {
   "user":{
      "uid":"some_user_id",
      "displayName":null,
      "photoURL":null,
      "email":"hshy@bshs.hs",
      "emailVerified":false,
      "phoneNumber":null,
      "isAnonymous":false,
      "providerData":[
         {
            "uid":"hshy@bshs.hs",
            "displayName":null,
            "photoURL":null,
            "email":"hshy@bshs.hs",
            "phoneNumber":null,
            "providerId":"password"
         }
      ],
      "apiKey":"some_api_key",
      "appName":"[DEFAULT]",
      "authDomain":"sub-dom.firebaseapp.com",
      "stsTokenManager":{
         "apiKey":"some_api_key",
         "refreshToken":"some_JWT_token",
         "accessToken":"some_JWT_token",
         "expirationTime":1541582223251
      },
      "redirectEventId":null,
      "lastLoginAt":"1541577292000",
      "createdAt":"1541577292000"
   },
   "credential":null,
   "additionalUserInfo":{
      "providerId":"password",
      "isNewUser":true
   },
   "operationType":"signIn"
}

【问题讨论】:

  • 听起来你的 data.user.emaildata.user.createdAt 是空的/没有值。 Firebase 只会存储具有值的键,因此如果这些值为空,则可以解释您所看到的情况。
  • 我已经更新了我的答案。如您所见,这些值不是空/空/未定义。它们实际上有一个值,并且可以在 auth 操作的结果中使用,但由于某种原因我不能使用它们。这真的很奇怪。

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


【解决方案1】:

你快到了。
代码中存在与您检索存储信息的方式有关的问题,这就是为什么它变成undefined 并且不存储的原因。
它会是这样的:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(function(authResponse){
      firebase.database().ref('Users').child('006').set({
        email: authResponse.user.email,
        createdAt: authResponse.user.metadata.creationTime
      }).then((data)=>{
        //success callback

        console.log('data ' , data);
      }).catch((error)=>{
        //error callback
        console.log('error ' , error)
      })
    }).catch(function(error) {
      //Handle error
    });

这里是 auth 方法的参考 - firebase.auth.createUserWithEmailAndPassword
对于返回对象 - firebase.User

【讨论】:

  • 我真的看不出区别。我的意思是,您只需在“then”参数列表中将“data”替换为“user”。行为完全一样。我将更新我的问题以查看从身份验证端点返回的内容(以及以什么格式)。
【解决方案2】:

问题出在以下的访问级别:

createdAt: data.user.createdAt

正如您在我的问题中看到的,它在 JSON 中可用,但由于某种原因我无法获得它的值,这就是导致缺少新记录和 this 的原因。我会赞成@Code.Warrior 的答案,因为它的方向是正确的,但我获得创建日期的方式如下:

.then(function(authResponse){
          firebase.database().ref('Users').child(authResponse.user.uid).set({
            email: authResponse.user.email,
            createdAt: authResponse.user.metadata.creationTime
          }).then((data)=>{
            //success callback

            console.log('data ' , data);
          }).catch((error)=>{
            //error callback
            console.log('error ' , error)
          })
        }).catch(function(error) {
          //Handle error
        });

它是进一步嵌套的一层。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2017-12-04
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多