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

92
Views
Problem while upgrading a code snippet from firebase v8 to firebase v9

I want to change this Firebase V8 code to Firebase V9. I have tried to change this code to v9 in several ways, but unfortunately, all of them were failure:

useEffect(() => {
    const unsubscribe = db.collection("chats").onSnapshot((snapshot) => {
    setChats(
        snapshot.docs.map((doc)=>({
            id:doc.id,
            data: doc.data(),
           }))
        )
    );

    return unsubscribe;
}, [])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The collection() and onSnapshot() are top-level functions in Firebase v9 SDK that can be directly imported from Firestore SDK. Try refactoring the code as shown below:

import { collection, onSnapshot } from "firebase/firestore";

useEffect(() => {
  const colRef = collection(db, "chats")

  onSnapshot(colRef, (snapshot) => {
    setChats(
      snapshot.docs.map((doc) => ({
        id: doc.id,
        data: doc.data(),
      }))
    )
  });

  return unsubscribe;
}, [])

Also checkout: Firestore: What's the pattern for adding new data in Web v9?

The documentation has examples of code written in both V8 and V9 syntax so you can just switch the tabs to Version 9 (Modular) and compare it with previous Version 8 (namespaced) syntax.

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!