0

I can't pull data from react native firebase, the hook always returns anyone. I'm not getting any errors either.

Firebase data is available but I can't seem to connect to firebase. I think we are not able to communicate with firebase. How can I understand this? It was a working function days ago

import { View, Text } from 'react-native'
import React,{useState,useEffect} from 'react'
import auth, { firebase } from '@react-native-firebase/auth';
import database from '@react-native-firebase/database'

export default function DataRef() {

    const [list, setList] = useState([]);

    function getComments() {

        //const user = firebase.auth().currentUser.uid;
        const db = firebase.database();
        const ref = db.ref('Post/');
    
        ref.once("value", snapshot => {
    
          li = [];
          if (snapshot.val()) {
            snapshot.forEach(child => {
              li.push({
                id: child.val().id,
                nameSurname: child.val().nameSurname,
                postTime: child.val().postTime,
                text: child.val().text,
                title: child.val().title,
                category: child.val().category,
                follow: child.child('Follow/count').val(),
                commentCount: child.child('comments').numChildren(),
              })
    
            });
            console.log(li)
            setList(li)
          }
        }
        )
      }

     useEffect(() => {
        getComments()
     }, [])
     
  return (
    <View>
      <Text>DataRef</Text>
    </View>
  )
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • your `data` is a function, so to invoke it you have to do `data()` (instead of `data` as you now do). – Frank van Puffelen Sep 20 '22 at 12:59
  • ı try but not still same @Frank van Puffelen – TURAN BİCAV Sep 20 '22 at 13:00
  • The `console.log(first)` will still logs `undefined` as data is fetched asynchronously. You can just render the data in your UI and it should show up once `setFirst()` executed. Checkout [this answer](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Dharmaraj Sep 20 '22 at 13:01
  • @Dharmaraj What should I do to see this data in the console? – TURAN BİCAV Sep 20 '22 at 13:13

0 Answers0