After successfully implementing the login functionality in my app, I am now attempting to retrieve the user's document in order to display their name on the page.
email = loginemail.value;
password = loginpassword.value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
alert("Thanks for signing in!");
const user = userCredential.user;
useremail.innerHTML = user.uid;
// ...
})
.catch((error) => {
const errorCodelogin = error.code;
const errorMessagelogin = error.message;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
alert("Looks like you're a new user - we created a new account for you!");
const user = userCredential.user;
// ...
})
.catch((error2) => {
const errorCoderegister = error2.code;
const errorMessageregister = error2.message;
pagebody.innerHTML = errorMessagelogin + "<br /><br />" + errorMessageregister;
});
});
const docRef = doc(db, "users", useremail.innerHTML);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log("Document data:", docSnap.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
I directly followed Firebase's documentation while using the const docRef, with my table name and doc id plugged in. However, an error is being displayed in the console:
Uncaught (in promise) FirebaseError: Invalid document reference. Document references must have an even number of segments, but users has 1.