【发布时间】: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.email和data.user.createdAt是空的/没有值。 Firebase 只会存储具有值的键,因此如果这些值为空,则可以解释您所看到的情况。 -
我已经更新了我的答案。如您所见,这些值不是空/空/未定义。它们实际上有一个值,并且可以在 auth 操作的结果中使用,但由于某种原因我不能使用它们。这真的很奇怪。
标签: javascript firebase react-native firebase-realtime-database firebase-authentication