Skip to content

Collection Query Snapshot changes and just snapshot changes are firing with mixed results #1975

Closed
@jimmykane

Description

@jimmykane

Version info

Angular: 7.1.3

Firebase: 5.7.0

**AngularFire:**5.1.1

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

No plunker possible

Steps to set up and reproduce

  1. In my service I have a call to a collection query by multiple where clauses and return back a single observable.
public getStreams(eventID: string, activityID: string, types: string[]): Observable<StreamInterface[]> {
    return combineLatest.apply(this, types.map((type) => {
      return this.afs
        .collection('events')
        .doc(eventID)
        .collection('activities')
        .doc(activityID)
        .collection('streams', ref => ref.where('type', '==', type))
        .snapshotChanges()
        .pipe(map((streamSnapshots) => { // @todo should be reduce
          return this.processStreamSnapshots(streamSnapshots)[0] // Get the first element of the return
        }))                                                      // since the return with equality on the query should only fetch one afaik in my model
    })).pipe(map((streams: StreamInterface[]) => {
      return streams.filter((stream) => !!stream)
    }))
  1. Then I have some other call that fetches everything from that collection
public getAllStreams(eventID: string, activityID: string): Observable<StreamInterface[]> {
    return this.afs
      .collection('events')
      .doc(eventID)
      .collection('activities')
      .doc(activityID)
      .collection('streams')
      .snapshotChanges()
      .pipe(map((streamSnapshots) => {
        debugger;
        return this.processStreamSnapshots(streamSnapshots);
      }))
  }

I am calling and subscribing first on the multiple where query of my service eg:

this.streamsSubscriptions.push(this.eventService.getStreams(this.event.getID(), activity.getID(), [DataLatitudeDegrees.type, DataLongitudeDegrees.type])

Than at some point later I subscribe also to the "all" list

this.eventService.getAllStreams(this.event.getID(), activity.getID());

However the 'all' call gets triggered 2 times.
One it returns the results of the:

this.streamsSubscriptions.push(this.eventService.getStreams(this.event.getID(), activity.getID(), [DataLatitudeDegrees.type, DataLongitudeDegrees.type])

... which in my logic is an array of 2

And the second time it returns as expected all the results. (Array of lets say 20)

What am I missing or is this a bug

I would expect only one call with just all the results

Sample data and security rules

No data

Debug output

** Errors in the JavaScript console **

** Output from firebase.database().enableLogging(true); **

** Screenshots **

Expected behavior

See description

Actual behavior

See description

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions