I've been attempting to retrieve the user UID instead of using the automatically generated document ID in Firebase/Firestore, but I'm encountering this error:
TypeError: firebase.auth(...).currentUser is null
This is the content of my index.js file:
// Setting up Firestore Cloud Database
var db = firebase.firestore();
function registerUser(){
const txtname = document.getElementById('txtuname').value;
const txtEmail = document.getElementById('txtemail').value;
const txtPass = document.getElementById('txtpass').value;
firebase.auth().createUserWithEmailAndPassword(txtEmail, txtPass).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
//alert(errorMessage);
}
console.log(error);
});
var uid = firebase.auth().currentUser.uid;
if(uid !=null){
db.collection("users").doc(uid).add({
UserName: txtname,
Email: txtEmail,
Password: txtPass
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
}