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

81
Visualizações
Fetch Single Data by Document Id Reactjs Firebase Web SDK 9

I want to fetch the data from single document by selected ID. I have try with function "child" and "orderByChild" but it seems did not work with web SDK 9. Could you please to help with the new format query of web version 9. Below is my code at table database:

<Link to={`/view/${id}`}>
    {id.data.dateCreate}
</Link>

And the code in "View" page:

const [user, setUser] = useState({});
const { id } = useParams();
useEffect(() => {
    db
      .orderByChild(`reports/${id}`)
      .get()
      .then((snapshot) => {
        if (snapshot.exists()) {
          setUser({ ...snapshot.val() });
        } else {
          setUser({});
        }
      });
  }, [id]);

Thank you so much for your help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use getDoc() function to fetch a single document as shown below:

import { doc, getDoc } from "firebase/firestore";

useEffect(() => {
  const fetchDocById = async () => {
    // Create DocumentReference
    const docRef = doc(db, "reports", id) // db = getFirestore()
    
    // Fetch document
    const docSnap = await getDoc(docRef)
    
    if (snapshot.exists()) {
      setUser({
        ...snapshot.val()
      });
    } else {
      setUser({});
    }
  }

  fetchDocById()
}, [id]);

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

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