我有这段代码:
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
关于javascript - 无法在 firebase 实时数据库中存储新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53182217/