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

167
Views
¿Cómo obtener un solo documento de una base de fuego v9 en la aplicación de reacción?

Tengo este useDocument Hook para obtener un solo documento de un Firebasev9 al pasar la identificación del documento. ¿Puedo obtener el documento pasando un slug en lugar de una identificación?

useDocument Hook

 import { useEffect, useState } from "react" // firebase import import { doc, onSnapshot } from "firebase/firestore" import { db } from "../firebase/config" export const useDocument = (c, id) => { const [document, setDocument] = useState(null) const [error, setError] = useState(null) // realtime document data useEffect(() => { const ref = doc(db, c, id) const unsubscribe = onSnapshot( ref, (snapshot) => { // need to make sure the doc exists & has data if (snapshot.data()) { setDocument({ ...snapshot.data(), id: snapshot.id }) setError(null) } else { setError("No such document exists") } }, (err) => { console.log(err.message) setError("failed to get document") } ) // unsubscribe on unmount return () => unsubscribe() }, [c, id]) return { document, error } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Si tiene el slug como un campo en los datos del documento, puede usar una consulta para obtener ese documento como se muestra a continuación:

 // Query to fetch documents where slug field is equal to given SLUG const q = query(collection(db, c), where("slug", "==", SLUG)) onSnapshot(q, snap => { if (!snap.empty) { const data = snap.docs[0].data() // snap.docs would be an array with 1 document // There could be multiple in case multiple posts have same slug by chance console.log(data) } else { console.log("No documents found with given slug") } })
about 4 years ago · Juan Pablo Isaza Report

0

podrías hacer algo como

 import { useEffect, useState } from "react" // firebase import import { doc, onSnapshot } from "firebase/firestore" import { db } from "../firebase/config" const useDocumentHook = (slug) => { const [doc, setDoc] = useState(null) useEffect(() => { const docRef = db.collection("posts").doc(slug) const unsubscribe = onSnapshot(docRef, (doc) => { setDoc(doc) }) return () => unsubscribe() }, [slug]) return doc }
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!