1

im trying to fetch data from firebase database, which im getting the data very well, but when i try to push all items received in an array, i obtain an array which i cannot iterate through like any other javascript array. here is my code:

var li = [];
  
  useEffect(() => {
    firebaseDB
      .database()
      .ref("collection")
      .on("value", (data) => {
        let all = data;
        
        all.forEach((element) => {
          
          li.push(element.val());
        });
      });
  }, []);
  console.log(li);

when i do console.log(li) , i get the li array which seems okay with the data, but when i try to do something with the array e.g. if i do console.log(li.length) , it prints 0 yet the array has multiple items in it. the problem is that my li array doesnt behave like an array. please assist me locate where the problem could be

1 Answer 1

1
 const [arr,setArr] = useState([]);
  
  useEffect(()=>{
  firebaseDB
  .database()
  .ref("collection")
  .on("value", (snapshot) =>{
    let data = [];
    snapshot.forEach((child)=>{
      data.push(child.val());
    })
   setArr(data);
  });
 
},[])
Sign up to request clarification or add additional context in comments.

Comments

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.