繁体   English   中英

无法在firebase实时数据库中存储新记录

[英]Can not store a new record in firebase realtime database

我有这段代码:

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'

立即添加新记录(当然,不是电子邮件和createdAt的实际值,而只是字符串)。 我已经尝试过JSON.stringify / .toString(),但是没有成功。 我真的不知道我需要做什么才能将实际值添加到传递给set方法的JSON中。

而一方面的缺陷就是我得到了这个

从中返回的格式和示例内容

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"
}

你快到了。
代码中存在一个问题,即您正在检索存储信息的方式,这就是原因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

结果是问题的原因是访问级别:

createdAt: data.user.createdAt

正如你可以在我的问题看,这是在JSON那里提供,但出于某种原因,我不能得到它的价值,这是什么原因导致缺乏新的记录与 我将upvote @ 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
        });

它是一个进一步嵌套的级别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM