-1

I have a firestore database that look like this: enter image description here

How to fetch these xpS in the loop so that I don't have to explicitly write xp1, xp2, etc in the below code. All these xp are maps. This is my code:

{Experience && Experience.map((Experience) => {
                  return (
                    <div className="text-white">
                      <p dangerouslySetInnerHTML={{ __html: Experience.xp1.desc }} />
                    </div>
                  );
                })}
const fetchExperience = async () => {
    await firebase
      .collection("portfolioV2")
      .doc("exp")
      .get()
      .then((docs) => {
        const data = docs.data();
        console.log(data);
        setExperience([...Experience, data]);
      })
      .catch((err) => {
        console.log("Error getting documents", err);
      });
  };

1 Answer 1

0

The exp is a Firebase document and they can be presented in JS as simple json objects. To loop over those EXs wothout knowing them is just loopint trough an json without knowing it's keys:

const fetchExperience = async () => {
  await firebase
    .collection("portfolioV2")
    .doc("exp")
    .get()
    .then((docs) => {
      const data = docs.data();

      //Here we loop trough the exp keys
      Object.keys(data).forEach((key) => {
        console.log("key", key);
        console.log("key data", data[key]);
      });

      console.log(data);
      setExperience([...Experience, data]);
    })
    .catch((err) => {
      console.log("Error getting documents", err);
    });
};

Sign up to request clarification or add additional context in comments.

3 Comments

its not from me, someone even downvoted the question also. :| one more doubt, now I'm facing this issue with this code in react <div>this displays in page. {Experience && Experience.map((exp) => { Object.keys(exp).forEach((key) => { return (<div className="text-white"> <p dangerouslySetInnerHTML={{__html: exp[key].desc, //neither this }} /> this doesn't displays in page. </div>); }); })} </div>
Can you pls write a separate question for your next error. You can share a link to it here. thx

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.