Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda