简体   繁体   English

创建用户身份验证后,我无法将数据保存到数据库

[英]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.

相关问题 AngularJs在验证后将用户数据保存到会话中 - AngularJs save user data into session after authentication 我如何将 json 数据保存到数据库中并在保存后进行 foreach? - How i can save json data into database and to foreach after save? 我无法将数据从角度保存到后端laravel数据库 - I cannot save data from angular to backend laravel database 验证错误后的Firebase用户数据 - Firebase user data after authentication error 创建 Firebase 用户并同时将数据添加到数据库中 - Creating Firebase User and simultaneously adding data into database Wixcode /数据库-在显示数据库中的数据之前对用户进行身份验证 - Wixcode/Database - Authentication of User before displaying data from database 单击下订单后将数据保存在数据库中 - Save data on database after click on place order 用户接受身份验证后,Dropbox无法转到上一页 - Dropbox cannot go to previous page after user accepts authentication Firebase 在使用 FirebaseAuthentication 创建用户后不会将用户数据保存到 Firestore - Angular 9 - Firebase does not save userdata to Firestore after creating user with FirebaseAuthentication - Angular 9 如何在不在数据库猫鼬上创建新对象的情况下保存数据? - How to save data without creating new object on database mongoose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM