0

I have an Array that is built within a subscription. Now I want to use the array to find a string in it. I tried .include() and .find(). Both return always false, even if the string is there.

Is this normal behavior and is there any workaround to at least iterate through it?

//array initialization in beginning
seatsReserved = new Array<String>();

//pushing data to the Array
this.dataService.getBookings().subscribe(res => {
  res.forEach(booking => {
    if(booking.Event_ID === this.eventId){
      this.seatsReserved.push(...booking.seats.split(","));
    }
  })
})


//Method that gets called
getBookings(): Observable<Bookings[]> {
const eventsRef = collection(this.firstore, 'Bookings');    
return collectionData(eventsRef, { idField: 'id'}) as Observable<Bookings[]>

Console.Log(this.seatsReserved) shows the Array the following:

enter image description here

The brackets at Array[] are empty, the length seems to be 0.

cbxxen
  • 41
  • 4
  • And you have verified that `this.eventId` has a value and that both are strings/numbers? not that one is string and one is number? What do console.logs tell you inside the foreach? – AT82 Nov 29 '21 at 10:03
  • Yes, if I `console.log(this.seatsReserved)` I get an Array with four entries. Interestingly, it only shows the data if I "open" the array with the arrow (before only `Array []`). I tried to copy the array to another Variable, same behavior. – cbxxen Nov 29 '21 at 10:14
  • What do you mean *open the array with the arrow*? – AT82 Nov 29 '21 at 10:18
  • I added a Picture of it. Not sure if this is normal. `console.log` of other arrays shows the data already in the brackets e.g. `Array[1,2,3]`. – cbxxen Nov 29 '21 at 10:23
  • That is normal firefox (looks like you are using that) console behavior. It tells you that it is an array and shows you the methods if you expands it. – AT82 Nov 29 '21 at 10:29
  • Ah okey, the function that is called and subscribed returns an Observable. Is it possible to convert it to a "normal" array that stores data even if the observable is unsubscribed? – cbxxen Nov 29 '21 at 10:46
  • I don't see anything related to assigning data to an observable in your code? – AT82 Nov 29 '21 at 10:48
  • The `getBookings()` method returns an observable. – cbxxen Nov 29 '21 at 10:54
  • Okay, it's been ages since I use firebase, so I can't remember how it works :D But it seems that your values are in the `this.seatsReserved` array, so what exactly is the issue there? – AT82 Nov 29 '21 at 11:01
  • hehe, I am just getting started with it :D So it is really odd. If, for example, I try `this.seatsReserved.includes("1-2")` the return is `false`. Or, `this.seatsReserved.forEach(x => console.log(x))` does not give me anything back. It seems that I can not access any of the array functions. – cbxxen Nov 29 '21 at 11:13
  • So where exactly are you trying to do for example `this.seatsReserved.includes("1-2")`? – AT82 Nov 29 '21 at 11:18
  • In an other method inside the component, so not inside the .subscribe() – cbxxen Nov 29 '21 at 11:25
  • 1
    OK, so this is probably a race condition? https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular You just need to work around it, subscribe where you actually need it. You can always use rxjs operators to do additional operations if you need it. But yeah, the data is available inside subscribe, that is how it just works. – AT82 Nov 29 '21 at 11:28
  • 1
    Thank you! I called the function in the `.subscribe()` and now the functions are accessible. – cbxxen Nov 29 '21 at 11:49

0 Answers0