[英]I cannot save data to the database after creating user authentication
在 firebase 上創建用戶身份驗證后無法將數據保存到數據庫,控制台上出現以下錯誤:“TypeError: firebase.auth (...). CurrentUser is null”。 似乎代碼在創建身份驗證后無法讀取用戶的 uid
編碼:
createUserButton.addEventListener('click', function(){
firebase.auth().createUserWithEmailAndPassword(emailInput.value, passwordInput.value)
var user = {
nome: "Pedro",
sobrenome: "Ribeiro",
cpf: "946.201.340-31",
uid: firebase.auth().currentUser.uid,
email: emailInput.value,
password: passwordInput.value
}
writeUserData(user)
.catch(function(error) {
// Handle Errors here.
})
})
function writeUserData(user) {
firebase.database().ref('users/' + firebase.auth().currentUser.uid).set(user).catch(error =>{
console.log(error.message)
})
}
需要更改什么以便代碼可以讀取用戶的 uid 並將數據保存在數據庫中?
在寫入數據庫之前,您無需等到新用戶創建過程完成。 createUserWithEmailAndPassword返回一個 promise,它在工作完成時解析。 它會給你一個UserCredential object 來使用。
firebase.auth().createUserWithEmailAndPassword(emailInput.value, passwordInput.value)
.then(userCredential => {
// write the database here
})
.catch(error => {
// there was an error creating the user
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.