Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
How do I add Firestore to my Javascript code?

In Javascript, I've been trying to add Firestore using the code below:

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());
    });
});

However, I get the following error:

Uncaught TypeError: db.collection is not a function

Please help, thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are using modular version so try refactoring the onSnapshot to this syntax:

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);
  });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!