1

I know it's a pretty simple question but I don't know why not work with me!

I'm declaring an array and after getting the data from the server I pushed them into these empty array and when I log them I can see my data like this

data logger

I'm using Loadash to get the unique obj so why not work for me?

here's my function

_uniqUser= () => {
    let currentUser = firebase.auth().currentUser.uid;
    let ref = firebase.database().ref(`users/${currentUser}`);
    let providersKeys = [];

    ref.once("value").then(snapshot => {
      snapshot.forEach(childsnap => {
        providersKeys.push(childsnap.key);
      });
      let providers = [];
      providersKeys.forEach(key => {
        firebase
          .database()
          .ref("providers")
          .child(key)
          .once("value")
          .then(providersShot => {
            let username = providersShot.val().username;
            providers.push({ username: username, key: key });
          });
      });
      let uniq = _.uniqBy(providers,"key")
      console.log(uniq); // I got Empty array
      console.log(typeof providers); // I got Object!!
      this.setState({ providers:uniq, noChat: false });
      // this.setState({ providers, noChat: false },()=>console.log(this.state.providers)) // I see the above image!
    });
  };
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Oliver D
  • 2,579
  • 6
  • 37
  • 80
  • 1
    Try `console.log(JSON.stringify(providers));` before `let uniq = _.uniqBy(providers,"key")` - what does it output? I have a feeling you're seeing the lazy-logged results, and that your array is actually empty at the time you're trying to manipulate it. You're not waiting on the async to finish before moving on and using the not-yet-populated `providers` array. – Tyler Roper Aug 21 '19 at 18:21
  • hmm, I get empty array `[]` when I log `console.log(JSON.stringify(providers))` and after line I log `console.log(providers)` I got my data!, So?@TylerRoper – Oliver D Aug 21 '19 at 18:31
  • @OliverD That's what I figured. The reason you see the array logged if you *don't* stringify it first, is because objects and arrays are lazily-evaluated in console. What you're seeing is **not** a reflection of the array at the time of logging. You can [read here](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) for more information. As for the reason it's empty? Perhaps [this](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) will help. – Tyler Roper Aug 21 '19 at 18:33
  • 1
    [Here is a simple JSFiddle](https://jsfiddle.net/qo1abLkz/) demonstrating the `console.log(array)` behavior I mentioned. Notice that I'm logging the array when it's empty and adding the items *after*, yet the `console.log` "incorrectly" shows 5 items when expanded. Long story short: If you ever want to see the true value of an array or an object, **stringify it**. – Tyler Roper Aug 21 '19 at 18:36
  • Sadly I didn't get it, to handle it in my code :( I don't use `async/await` before @TylerRoper – Oliver D Aug 21 '19 at 18:54
  • Please, if you can manipulate my Code to achieve this point! I'm very stuck @TylerRoper – Oliver D Aug 21 '19 at 23:42

0 Answers0