Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda