0

I am arrays of objects from multiple firestore collections and storing them all in an object. I want to render the individual objects in the arrays is the object. This is how I fetch my data:

  const categories = {
    Pakoras: [],
    Vegitarisches: [],
    "Biryani Gerichte": [],
    Hähnchengerichte: [],
    "Indische Beilagen": [],
    Lammgerichte: [],
    "Ben & Jerrys": [],
    Getränke: [],
    Pizza: [],
    Kindermenüs: [],
    Spaghetti: [],
    Tortellini: [],
    Gnochi: [],
    Maccheroni: [],
    Tagliatelle: [],
    "Überbackene Nudeln": [],
    Salate: [],
    Fleischgerichte: [],
    "Schnitzel & Fleischgerichte": [],
    Risotto: [],
    "Überbackenes-Gemüse-Kartoffeln": [],
    "Chinesische-Gerichte-Gebratene-Nudeln-Reis": [],
  }
   const [foods, setFoods] = useState(categories)
  useEffect(() => {
    fetch()
  }, [])
  function fetch(){
      console.log("Fetching")
      Object.keys(categories).forEach(category => {
        fire.firestore().collection(category).get()
          .then(snapshot => {
            var food = []
            snapshot.forEach(doc => {
              food.push(doc.data())
              //console.log(doc.data())
            })
            categories[category] = food;
          }).catch(error => {
            console.log(error)
          })
      })
      //console.log(foods)
    }

A doc.data() object looks something like this:

    var example = {
        title: "example",
        price: 10,
        desc: "none"
    }

The objects are in an array under the respective category. How do I render this? I want to loop through everything and render it like this:

Category1:

  • title - price - desc
  • title - price - desc

Category2:

  • title - price - desc
  • title - price - desc

1 Answer 1

1

Object.entries(categories) will allow you to iterate an array of key/value pairs from your object.

In your case the key would be the category and the value would be the array of foods in that category.

Object.entries(categories).map(([category, foods]) => {
  return (
    <div key={category}>
      <h1>{category}</h1>
      <ul>
        { foods.map(food => (
            <li key={food.title}>
               {food.title} - {food.price} - {food.desc}
            </li>
        )}
      </ul>
    </div>
  )
})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. It works at first, but when I refresh the page the categories can bee seen but not the food's. Is this a firebase error?
They haven't loaded yet. You're rendering the initial structure with all the empty arrays and you're never calling setFoods so the component never re-renders.
Ah ok thanks. I found another solution with arrays instead of objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.