Home > Enterprise >  Firebase - how to retrieve post data only where the slug URL matches a key in the object?
Firebase - how to retrieve post data only where the slug URL matches a key in the object?

Time:07-20

I am very new to firebase and I am unable to fetch the blog port with the help of Slug key value. I tried out few things but all were unsuccessful.

Can anyone help me out in fetching the data please?

I want to fetch my blog post data with the help of Sulg URL, not behalf of key. See the image below

I have this key value quickly-extract-email-address-from-text

with the help of ID i am getting this blog post data.

 const [article, setArticle] = useState([]);

useEffect(() => {

    const docRef = doc(db, "BlogArticles", id);
    onSnapshot(docRef, (snapshot) => {
        setArticle({ ...snapshot.data(), id: snapshot.id });
    });

    
}, []);

enter image description here

but i want the same data with the help of this key value quickly-extract-email-address-from-text

enter image description here

CodePudding user response:

It sounds like you're asking how to execute a query against Firestore. Based on that, you'll want something like this for the query:

const articlesRef = collection(db, "BlogArticles");

const q = query(citiesRef, where("slug", "==", "quickly-extract-email-addresses-from-text"));
onSnapshot(q, (snapshot) => {
  snapshot.forEach((doc) => {
    setArticle({ ...doc.data(), id: doc.id });
  })
});

Keep in mind that if your database would ever contains multiple documents matching the query, this would only end up showing the last of those matches.

CodePudding user response:

Hey frank van thanks for the replay man

 useEffect(() => {
        const articlesRef = collection(db, "BlogArticles");
        const q = query(articlesRef, where("slug", "==", 'quickly-extract-email-address-from-text'));
        onSnapshot(q, (snapshot) => {
            const articles = snapshot.docs.map((doc) => ({
                id: doc.id,
                ...doc.data(),
            }));
            setArticle(articles);
        });
    }, []);

i am not getting any return value , []

  • Related