I am a new developer with no experience in Web apps. I have a collection called “users” in Firestore in which the document IDs are the user’s emails. I am trying to read the data in one of the documents following the example provided by Firebase here
I get two errors:
- First one is the use of “await”: Uncaught SyntaxError: Unexpected reserved word.
- Second is this message: Uncaught TypeError: docSnap.exists is not a function at HTMLFormElement.
I bypassed the first one by ommiting “await”, but cannot avoid the second. Have you got any idea of what is wrong with my code?
console.log(docSnap) gives the following message: “Promise {pending}”
Thanks.
JavaScript
x
12
12
1
requestForm.addEventListener('submit', (event) => {
2
event.preventDefault();
3
4
const user = auth.currentUser;
5
const docSnap = await getDoc(doc(db, "users", user.email));
6
7
if (docSnap.exists()) {
8
console.log("Document data:", docSnap.data());
9
} else {
10
console.log("No such document!");
11
}
12
})
Advertisement
Answer
Looks like you return a Promise
and it does not meet the requirements of async/await
. Try this way.
JavaScript
1
8
1
getDoc(doc(db, "users", user.email)).then(docSnap => {
2
if (docSnap.exists()) {
3
console.log("Document data:", docSnap.data());
4
} else {
5
console.log("No such document!");
6
}
7
})
8