简体   繁体   中英

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.

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); }
});

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