我有这段代码:
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"
}
最佳答案
你快到了。在代码中,您检索存储信息的方式存在问题,这就是为什么结果显示为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