Home > OS >  How to get the data of a query from firebase database in react-native
How to get the data of a query from firebase database in react-native

Time:10-21

I am trying to list the data of each document that i am fetching from a firebase query, However I am being able to show the data through flatlist, Iwoudl love to iterate the data in another way in order to add it to a PDF that will be created from the app.

const [Annotations, setAnnotations] = useState(null);
useEffect(() => {
    const q = query(
      collection(db, "notifications"),
      orderBy("createdAt", "desc")
    );
    console.log("Query:");
    console.log(q);
    onSnapshot(q, (test) => {
      setAnnotations(test.docs);
    });

  }, []);

From the code above I am getting some unneccesary data, however I’d like to get the data of the document, hope you help me, thanks!

CodePudding user response:

What you're seeing are the internals of the Firestore DocumentSnapshot and QueryDocumentSnapshot classes, which is indeed pretty meaningless.

If you only want to use the data:

onSnapshot(q, (test) => {
  setAnnotations(test.docs.map((doc) => doc.data()));
});

If you want the data and document ID:

onSnapshot(q, (test) => {
  setAnnotations(test.docs.map((doc) => { doc.id, ...doc.data() }));
});
  • Related