Using rxjs Observed with firestore

I am trying to create an observable rxjs in the Query # onSnapshot method. This is my code:

let observable = Rx.Observable.create(db.collection('conversations')
     .where('members.' + auth.currentUser.uid, '==', true).onSnapshot)
observable.subscribe({next(value){console.log('value', value)}})

The error I am getting is this:

TypeError: this.onSnapshotInternal is not a function

It seems that the onSnapshot method is set as duck-typed as observable. Firestore doesn't have enough documentation for me to figure this out.

+4
source share
1 answer

When you pass onSnapshotin Rx.Observable.create, you pass it unbound to the request. That is, you just pass the function Query.prototype.onSnapshot.

You can use bind, for example:

const query = db
  .collection('conversations')
  .where('members.' + auth.currentUser.uid, '==', true);
let observable = Rx.Observable.create(query.onSnapshot.bind(query));
observable.subscribe({
  next(value) { console.log('value', value); }
});

Or you can use the arrow function, for example:

let observable = Rx.Observable.create(observer => db
  .collection('conversations')
  .where('members.' + auth.currentUser.uid, '==', true)
  .onSnapshot(observer)
);
observable.subscribe({
  next(value) { console.log('value', value); }
});
+7
source

Source: https://habr.com/ru/post/1687125/


All Articles