I am trying to get all the documents in a subcollection by following the firebase documentation, however the error 'await is an reserved identifier' appears.
This is my code currently and I do not see where 'async' could be used with await and the documentation does not indicate that it would be used.
getAuth().onAuthStateChanged((user) => {
if (user) {
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
//reference to the subcollection of subjects in the user's document
const subjectRef = collection(db, "users", auth.currentUser.uid, "subjects");
const querySnapshot = await getDocs(subjectRef);
querySnapshot.forEach((doc) => {
console.log(doc.id, "=>", doc.data());
});
}
});
I have tried getting all the documents with db.collection.('users').document(auth.currentUser.uid).collection('subjects').get() where db = getFirestore(app), however this does not work as the error
'db.collection is not a function' appears and any soloutions I have found to it are not relevant as db is refering firestore not the real time database.
You need to make the callback async:
getAuth().onAuthStateChanged(async (user) => {
if (user) {
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
//reference to the subcollection of subjects in the user's document
const subjectRef = collection(db, "users", auth.currentUser.uid, "subjects");
const querySnapshot = await getDocs(subjectRef);
querySnapshot.forEach((doc) => {
console.log(doc.id, "=>", doc.data());
});
}
});