#Firebase results not showing in frontend

8 messages · Page 1 of 1 (latest)

mental oxide
#

Hello,
I have a collection in Firebase that has some categories with a title and a type. When I import the collection in my astro file and console log it, I get the results, but the results are not appearing on the front end...

tacit furnaceBOT
#

Hi @mental oxide. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Hmm, I don't know enough to give you a confident answer yet. However, I can guide you on how to display data from a Firebase collection in an Astro file based on the information provided in the extracts.

First, ensure that you have correctly initialized Firebase in your Astro project and added the necessary Firebase credentials. You can refer to the Adding Firebase credentials (https://docs.astro.build/en/guides/backend/google-firebase/) guide for more details.

Next, you need to connect to your Firestore database and fetch the data from your collection. Here's an example of how to fetch data from a collection named "friends":

---import { app } from "../firebase/server";
import { getFirestore } from "firebase-admin/firestore";

const db = getFirestore(app);
const friendsRef = db.collection("friends");
const friendsSnapshot = await friendsRef.get();
const friends = friendsSnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data(),})) as Friend[];---

Finally, you need to display the fetched data on the front end. Here's an example of how to display a list of friends:

<Layout title="My friends">
  <h1>Friends</h1>
  <ul>
    { friends.map((friend) => (
      <li>
        <a href={`/friend/${friend.id}`}>{friend.name}</a>
        <span>({friend.age})</span>
        <strong>{friend.isBestFriend ? "Bestie" : "Friend"}</strong>
        <a href={`/edit/${friend.id}`}>Edit</a>
      </li>
    )) }
  </ul>
</Layout>
#

If you're still having trouble displaying the data on the front end, it might be due to an error in your code. Please check your console for any error messages. If you're not seeing any errors, it might be helpful to share more details about your code and the specific issue you're encountering.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@mental oxide kapa.ai is still learning and improving, please let us know how it did by reacting below

mental oxide
#

@tacit furnace ,This is my code:

---
import type { Category } from "@lib/types";
import { firestore } from "@lib/firebase/server";

/* Get categories from firestore */
const categoryQuery = await firestore.collection("categories").get();
const categories = categoryQuery.docs.map((doc) => {
  return { id: doc.id, ...doc.data() };
}) as Category[];
console.log(categories);
---

{
  categories && categories.length > 0 ? (
    categories.map((cat) => {
      if (cat.type === "income") {
        <CategoryItem
          title={cat.title}
          type={cat.type}
          classes='text-green-600'
        />;
      } else {
        <CategoryItem
          title={cat.title}
          type={cat.type}
          classes='text-red-600'
        />;
      }
    })
  ) : (
    <tr class='bg-slate-50 dark:bg-slate-800 border-b text-red-600 dark:border-slate-700'>
      <th class='px-6 py-3'>Geen categorieën gevonden</th>
      <td class='px-6 py-3'>-</td>
    </tr>
  )
}
tacit furnaceBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the code you've provided, it seems like you're missing the return keyword in your map function. This is likely why your categories are not being displayed on the front end.

In JavaScript, the map function creates a new array with the results of calling a provided function on every element in the array. If you're using curly braces {} for the function body, you need to explicitly use the return keyword to specify the value to be returned.

Here's how you can fix your code:

{
  categories && categories.length > 0 ? (
    categories.map((cat) => {
      if (cat.type === "income") {
        return (
          <CategoryItem
            title={cat.title}
            type={cat.type}
            classes='text-green-600'
          />
        );
      } else {
        return (
          <CategoryItem
            title={cat.title}
            type={cat.type}
            classes='text-red-600'
          />
        );
      }
    })
  ) : (
    <tr class='bg-slate-50 dark:bg-slate-800 border-b text-red-600 dark:border-slate-700'>
      <th class='px-6 py-3'>Geen categorieën gevonden</th>
      <td class='px-6 py-3'>-</td>
    </tr>
  )
}

This should solve your issue and display the categories on the front end.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: