0

I was working in a vue project where I used a similar statement, and it worked nicely, now I'm need to do something similar with pure javascript for other project with Firebase but is not working. What I'm doing wrong, want to try to find the id inside the collection and then would like to get the group array. So the match(code below returns undefined)

//making snapshot to the realtime database  

var dbValues = [ ];
var userOnStream = firebase.database().ref('users');
userOnStream.once('value', (childSnapshot) => {
  var data = childSnapshot.val();
  dbValues.push(data);
});


var userId = "0001"

var match = dbValues.find((x) => x.id == userId);
console.log(match); // this returns undefined
console.log(dbValues);  //this returns correctly the array

And here you can find a sample of my data structure

{
    "users" : [
      {
      "email" : "user3@emails.com",
      "group": [ "slt1", "s1/index.html","Tue Jun 09 2021 10:00:00 GMT+0200 (Central European Summer Time)"],
      "id" : "0001"   
    }, {
      "email" : "user2@emails.com",
      "group": [ "slt2", "s2/index.html","Tue Jun 09 2021 11:00:00 GMT+0200 (Central European Summer Time)"],
      "id" : "0002"
      
    }
    ...
  ]
  }

Thank you for help an advice

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
druj_nasu
  • 131
  • 8
  • 2
    `dbValues` values isn't populated by the time your `.find()` method runs, nor is it when you do your `console.log(dbValues);` (use `console.log(JSON.stringify(dbValues))` to see the actual array value at the time of logging it) – Nick Parsons Jun 08 '21 at 10:52
  • 1
    Also see [is-chromes-javascript-console-lazy-about-evaluating-arrays](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) for the console behavior. – ASDFGerte Jun 08 '21 at 10:52
  • @NickParsons Thank you, I tried this early with the dbValues but I just get on the console [ ] – druj_nasu Jun 08 '21 at 11:01
  • var match = (dbValues. users).find((x) => x.id == userId); use this – ramlalsharma1994 Jun 08 '21 at 11:08
  • Hello @ramlalsharma1994 thank you, but it did not work, also it seems someone closed my post, I'm wondering if is related to the way the snapshot itself is being call from the database :( – druj_nasu Jun 08 '21 at 11:22
  • @druj_nasu you need to move your code that relies on `data`/`dbValues` inside of your callback function – Nick Parsons Jun 08 '21 at 11:28
  • Also, [`.once()`](https://firebase.google.com/docs/reference/js/firebase.database.Reference#once) only triggers once, so pushing data into dbValues will only every result in one value being pushed. You seem like you might want to use `data.users.find(...);` – Nick Parsons Jun 08 '21 at 11:32
  • Hey @NickParsons the before last comment gave me the hint :) thank you and just in case I changed to on() still didn't see to make a difference, so what I did to keep that outside and use it in another part of my code and site was save it as an item in a session storage, not sure if that's actually a good idea but it works – druj_nasu Jun 08 '21 at 12:40

0 Answers0