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

时间:2018-11-07 00:47:44

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

我有这段代码:

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)
“则”参数列表中的

是:

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

2 个答案:

答案 0 :(得分:3)

您快到了。
在代码中,您检索存储信息的方式存在问题,这就是为什么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

答案 1 :(得分:0)

事实证明,问题在于访问级别:

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
        });

它是进一步嵌套的一级。