En Javascript, he estado tratando de agregar Firestore usando el siguiente código:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js"; import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-analytics.js"; import { getFirestore } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-firestore.js"; const firebaseConfig = { apiKey: "XXXXX", authDomain: "XXXXX", projectId: "XXXXX", storageBucket: "XXXXX", messagingSenderId: "XXXXX", appId: "XXXXX", measurementId: "XXXXX" }; const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); const db = getFirestore(); db.collection("users").where("uid", "==", "1234") .onSnapshot((snapshot) => { snapshot.docChanges().forEach((change) => { console.log("change ", change.doc.data()); }); });Sin embargo, me sale el siguiente error:
TypeError no detectado: db.collection no es una función
Por favor ayuda, gracias.
Está utilizando una versión modular, así que intente refactorizar onSnapshot a esta sintaxis:
import { collection, query, where, onSnapshot } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-firestore.js"; const q = query(collection(db, "users"), where("uid", "==", "1234")); const unsubscribe = onSnapshot(q, (querySnapshot) => { const documents = []; querySnapshot.forEach((doc) => { documents.push(doc.data().name); }); });