I implemented a function where I fetch all Docs from a Firebase collection on a click.
Now I want to display each doc I fetched in a <div>
container in JSX. When I try to take the array and display it, I´m getting the error that the array is not found.
This is my code:
async function getAllDivs(){
const querySnapshot = await getDocs(collection(db, "Div"))
const allDivs = [];
querySnapshot.forEach(doc => {
allDivs.push(doc.data().DivContent);
});
}
2
Answers
You would have to return the array from the function, because of the "scope".
Example:
Also, I suggest against pushing data to an array labeled as
const
, as it could be confusing for someone else reading your code.I think you could use something like this:
If
DivContent
contains HTML you can usedangerouslySetInnerHTML
.