Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an rxjs Observable with firestore

I am trying to create an rxjs observable on 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 receive is this:

TypeError: this.onSnapshotInternal is not a function

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

like image 436
Joseph Chavez Avatar asked Oct 09 '17 01:10

Joseph Chavez


1 Answers

When you pass onSnapshot to Rx.Observable.create, you are passing it unbound to a query. That is, you are just passing the Query.prototype.onSnapshot function.

You could either use bind, like this:

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 could use an arrow function, like this:

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); }
});
like image 63
cartant Avatar answered Nov 07 '22 23:11

cartant