My js file is correctly entered in the DOM, I have initailized firebase in the firebase.js file. Why am I getting a
TypeError 'collection(...).doc is not a function' -
The collection query is taken directly from the firebase docs site, I don't understand how this could be a type error.. Any ideas?
import { app } from "./firebase";
import { getFirestore, doc, collection } from "firebase/firestore";
const db = getFirestore(app);
// get data
const docRef = collection(db, 'posts').doc('ARt1ctrEjweKEd4gmgCr');
await docRef.get();
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
CodePudding user response:
The doc()
is top level function in Modular SDK and not a method on collection()
. Try:
import { getFirestore, doc, getDoc } from "firebase/firestore";
// Creating DocumentReference
const docRef = doc(db, 'posts', 'ARt1ctrEjweKEd4gmgCr');
const docSnap = await getDoc(docRef);
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
Also checkout: Firestore: What's the pattern for adding new data in Web v9?