FirebaseExtended / angularfire

AngularJS bindings for Firebase
MIT License
2.73k stars 632 forks source link

limitToFirst() doesn't fire #963

Closed bnfrnz closed 6 years ago

bnfrnz commented 6 years ago

angular "5.0.0", ionic-angular "^3.9.2", firebase "^4.7.0", angularfire2 "5.0.0-rc.4",

Test case

I'm getting the first key of a list of notifications to determine whether my pagination logic should continue querying or I have reached the first key:

getFirstNotificationKey$(userKey: string): Observable<string | null> {

    return this.afDb.list<INotification>(`userNotifications/${userKey}`, ref => ref
        .orderByKey()
        .limitToFirst(1))
        .valueChanges()
        .pipe(
            map((iNotifications: INotification[]) => iNotifications.length > 0 ? iNotifications[0].key : null)
        );

}

When I subscribe to this, it fires as expected and delivers the first key. But when I then remove the respective item in the database. it does not fire again. When I readd the item, it also doesn't fire again. It basically only fires once. It's like later child removed and child added etc. events are being ignored.

Am I missing something? Is this expected behavior for some reason?

Steps to reproduce

Add a few objects in firebase rtdb. Take the above code, subscribe to the observable, and log the result to the console. Then remove the first object in the list... nothing fires.

Expected behavior

When the list changes, ie. the first object is removed or another one is inserted before it (so, the first object is now a different one), I would expect the observable to fire and give me the new first object key or null in case the list is now empty.

Actual behavior

Subscription does not fire (see above).

bnfrnz commented 6 years ago

No one?

I still can't get this to work. Even when specifically asking for all events, it doesn't fire.

.valueChanges(['child_added', 'child_removed', 'child_changed', 'child_moved'])

After receiving the first item once, it doesn't fire anymore when the item is then removed or a new first item is inserted.

How can I subscribe to the first item of a list then? Any ideas?

EDIT: As workaround, .orderByChild('timestamp').limitToFirst(1) fires as expected. Just .orderByKey().limitToFirst(1) does not work.

davideast commented 6 years ago

Hey @bnfrnz! You are in the wrong repo. This is AngularFire for AngularJS. Go to the AngularFire for Angular repo for help: https://github.com/angular/angularfire2

bnfrnz commented 6 years ago

Oops, my bad