I'm using firestore to store some data in two collections ("users" and "requests"). I have some functions in my javascript code that allow me to read and write the data in the database, and they work fine. However, when I use the same methods in the following code, I get two errors:
I've tried several options but they don't work and I'm very confused, because the code I use to access the data works fine in other parts of the program.
document.addEventListener('DOMContentLoaded', function() {
const user = auth.currentUser;
getDocs(collection(db, "requests")).then(querySnapshot => {
querySnapshot.forEach((doc) => {
document.getElementById(doc.id).addEventListener('click', function() {
openAcceptModal();
})
})
})
getDoc(doc(db, "users", user.email)).then(docSnap => {
if (docSnap.exists()) {
const uEmail = docSnap.get("email");
const uName = docSnap.get("name");
const uPost = docSnap.get("post");
const uRating = docSnap.get("rating");
const uTeam = docSnap.get("team");
console.log(uEmail,uName,uPost,uRating,uTeam);
}
})
})
Does anybody have any idea of what's wrong with this?
The First error “Cannot read properties of null (reading 'email') at HTMLDocument” is mainly caused if the current user is not signed in and it will take the null value, so the user will not be able to access the ‘email’. You need to check whether the user is signed in or not, you can refer to the stackoverflow answer where an example has been provided.
The second error “Cannot read properties of null (reading 'addEventListener')” is mainly resolved by checking the variable before adding an event listener. You can refer to the stackoverflow answer where the issue has been resolved by the Rob.