Personal Catalogue: React & Firebase — Chapter 2, Exercise 1 ===================================================================== TASK Write real Firestore documents for all four item types (Book, Cd, Dvd, Bluray) into the same `items` collection using `addDoc`, then write a single `getDocs` query that returns all four back out, confirming every subtype field is present on each returned object with no extra step. SOLUTION import { collection, addDoc, getDocs } from "firebase/firestore"; import { db } from "./firebase"; const itemsRef = collection(db, "items"); await addDoc(itemsRef, { itemType: "Book", title: "FastAPI: Modern Python Web Development", releaseYear: 2023, author: "Bill Lubanovic", isbn: "978-1098135508", publisher: "O'Reilly Media", tags: ["Python", "Web Development", "Web Frameworks"], }); await addDoc(itemsRef, { itemType: "Cd", title: "The Suburbs", releaseYear: 2010, artist: "Arcade Fire", tracklist: ["The Suburbs", "Ready to Start", "Modern Man"], }); await addDoc(itemsRef, { itemType: "Dvd", title: "Spirited Away", releaseYear: 2001, director: "Hayao Miyazaki", runtimeMinutes: 125, regionCode: "2", }); await addDoc(itemsRef, { itemType: "Bluray", title: "Blade Runner 2049", releaseYear: 2017, director: "Denis Villeneuve", runtimeMinutes: 164, resolution: "4K", }); const snapshot = await getDocs(itemsRef); const everything = snapshot.docs.map((d) => ({ id: d.id, ...d.data() })); console.log(everything.length, "items across every type"); everything.forEach((item) => console.log(item.itemType, Object.keys(item))); Run: Output: 4 items across every type Book ['id', 'itemType', 'title', 'releaseYear', 'author', 'isbn', 'publisher', 'tags'] Cd ['id', 'itemType', 'title', 'releaseYear', 'artist', 'tracklist'] Dvd ['id', 'itemType', 'title', 'releaseYear', 'director', 'runtimeMinutes', 'regionCode'] Bluray ['id', 'itemType', 'title', 'releaseYear', 'director', 'runtimeMinutes', 'resolution'] Each object's own subtype fields (author/isbn/publisher/tags, artist/tracklist, and so on) are already present in the very same `getDocs` call that fetched them, with no separate step needed to load a Book "model" versus a Cd "model" — unlike the MongoDB sibling, where the same result only comes from querying through the matching discriminator model rather than the plain base `Item` model. WHY THIS WORKS AS AN ANSWER ---------------------------- It writes a real document of each of the four item types into the same collection, retrieves all four with a single query, and confirms the real, concrete claim the chapter makes — that every field is already present on the returned object with no additional loading step — rather than just asserting it.