Search code examples
javascriptfirebasereact-nativegoogle-cloud-firestorereact-native-firebase

how can i pass data from firestore to a function in react-native


Here is the function i want to pass the data to

 function db(threads) {
    setThreads(threads);
    if (initializing) setInitializing(false);
  }

Here is my query of the data

useEffect(() => {
  firestore().collection('User').onSnapshot(querySnapshot => {
    const list = [];
    querySnapshot.forEach(doc => {
      const {  id, name  } = doc.data();
      list.push({
        _id: doc.id,
        id,
        name,
        // add this
        latestMessage: {
          text: '',
          userName: '',
          avatar: '',
        },
      });
    });
    db(list); });
}, []);

Please help me out, I've been on this for a while.

I use the latest versions of react-native and firebase.

It keeps showing me this error

TypeError: null is not an object (evaluating '_useContext7.threads')

But when I logged them to the console, I could see all the data from the database there.

i just need the threads.id and threads._id


Solution

  • Ciao, try to modify your useEffect like this:

    useEffect(() => {
    firestore().collection('User').onSnapshot(querySnapshot => {
    const list = [];
    querySnapshot.data().forEach(doc => {
      list.push({
        _id: doc.id,
        id: doc.id,
        name: doc.name,
        // add this
        latestMessage: {
          text: '',
          userName: '',
          avatar: '',
        },
      });
    });
    db(list); });
    }, []);