[英]I cannot save data to the database after creating user authentication
I cannot save data to the database after creating user authentication on firebase, the following error appears on the console: "TypeError: firebase.auth (...). CurrentUser is null".在 firebase 上创建用户身份验证后无法将数据保存到数据库,控制台上出现以下错误:“TypeError: firebase.auth (...). CurrentUser is null”。 it seems that the code cannot read the user's uid after creating the authentication似乎代码在创建身份验证后无法读取用户的 uid
the code:编码:
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)
})
}
what needs to be changed so that the code can read the user's uid and save the data in the database?需要更改什么以便代码可以读取用户的 uid 并将数据保存在数据库中?
You're not waiting until the new user creation process is complete before writing the database.在写入数据库之前,您无需等到新用户创建过程完成。 createUserWithEmailAndPassword returns a promise which resolves when the work is done. createUserWithEmailAndPassword返回一个 promise,它在工作完成时解析。 It will give you a UserCredential object to work with.它会给你一个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.