This is my first time asking a question on here, usually I can find what I'm looking for, but I am upgrading from Firebase 8 to Firebase 9 in an ionic/react FC app, and have got some of it figured out, but I cannot figure out how to get a single document from nested collections. In Firebase 8 it would look like:
db.collection('department').doc(deptId).collection('employees').doc(empId).get()...
I have tried a couple ways with collectionGroup, and don't get anything returned at all, but I'm not even sure that is the correct way to go. Any help would be greatly appreciated!! Thanks!
If you already know syntax (and how Firebase works) of Firebase JS SDK (V8 and before), I'll highly recommend checking out the documentation as they have syntax of both side by side. Just click on the modular tab in documentation. Fetching a single document will be as shown below:
import { doc, getDoc } from "firebase/firestore";
const docRef = doc(db, "department", deptId, "employees", empId);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log("Document data:", docSnap.data());
} else {
console.log("No such document!");
}
Also checkout: Firestore: What's the pattern for adding new data in Web v9?