简体   繁体   中英

Angular | Subscribe to multiple observables

I'm trying to create a list of events that a user is going to. First I get event keys and then what I would like to do is subscribe to each event and listen for changes. Currently only the last event works because this.eventRef is being changed in the for loop.

eventRef: AngularFireObject<any>


getEvents() {

const eventsGuestsLookup = this.db.object(`eventsGuestsLookup/${this.uid}`).valueChanges()

this.eventsGuestsLookupSub = eventsGuestsLookup
  .subscribe(eventKeys => {

    if (eventKeys) {
      console.log(eventKeys)
      for (const k in eventKeys) {
        if (eventKey.hasOwnProperty(k)) {

          this.eventRef = this.db.object(`events/${k}`)

          console.log(this.eventRef)

          this.eventRef.snapshotChanges().subscribe(action => {

            const key = action.payload.key
            const event = { key, ...action.payload.val() }

           this.makeEvents(event)

          })
        }
      }
    }
  })

}

What I do next is get the user's response and for each status I want to display certain information. I don't know any other way of doing this, so I check both lists attending and notAttending and if there is a response from the user I change the event properties.

makeEvents(event) {
console.log(event)

event.goingText = "RSVP"
event.setGoing = 'rsvp'
event.setColor = "rsvp-color"

const attending = this.db.object(`attendingLookup/${this.uid}/${event.key}`).valueChanges()

this.attendingLookupSub = attending
  .subscribe(data => {
    console.log('attending', data)
    if (data) {
      event.goingText = "ATTENDING"
      event.setGoing = 'thumbs-up'
      event.setColor = 'attending-color'
    }
  })

const notAttending = this.db.object(`not_attendingLookup/${this.uid}/${event.key}`).valueChanges()

this.notAttendingLookupSub = notAttending
  .subscribe(data => {
    console.log('not attending', data)
    if (data) {
      event.goingText = "NOT ATTENDING"
      event.setGoing = 'thumbs-down'
      event.setColor = 'not-attending-color'
    }
  })

this.events.push(event)

}

*** Edit

const eventsGuestsLookup = this.db.object(`eventsGuestsLookup/${this.uid}`).valueChanges()
eventsGuestsLookup.subscribe(keys => {
  of(keys).pipe(
    mergeMap(keys => {
      Object.keys(keys).map(k => {
        console.log(k)
      })
      return merge(Object.keys(keys).map(k => this.db.object(`events/${k}`)))
    })
  ).subscribe(data => console.log('data', data))
})

what you want to acheive is flat your observables collection. to acheive it you can do something like this :

//Dummy eventKeys observable.
const obs1$ = new BehaviorSubject({key:1, action: 'lorem'});
const obs2$ = new BehaviorSubject({key:2, action: 'lorem'});
const obs3$ = new BehaviorSubject({key:3, action: 'lorem'});

const eventKeys = {
  obs1$,
  obs2$,
  obs3$
};
// Dummy eventsGuestsLookup observable.
of(eventKeys)
.pipe(
  //eventsGuestsLookup dispatch collection of obserbable, we want to flat it.
  mergeMap(ev => {
    // We merge all observables in new one.
    return merge(...Object.keys(ev).map(k => ev[k]));
  }),
).subscribe(console.log);

inportant note : ev[k] is an Observable object. On your case you should do something like :

.map(k => this.db.object(`events/${k}`)) // will return observable.

live demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM