Skip to content

Different subscription behaviour for mongo collections #1939

Open
@mattiLeBlanc

Description

@mattiLeBlanc

If was playing with my subscription of a mongo collection, doing search on a third party api that resulted in inserting records in my mongo collection. However, I noticed that I didn't see my subscription working in my template when I trigger the mongo insert while using one method, but it did work with another method.

Again for me this is confusing, what is the best practice and/or is this behaviour on purpose?

This subscription auto updates when I add new records manually or trigger the api that inserts mongo records:

export class PodcastSearchComponent implements OnInit {
  episodeSubscription: Subscription;
  episodes$: Observable<Episode[]>;

  constructor(
  ) {}

  ngOnInit() {
     this.episodeSubscription = MeteorObservable.subscribe( 'episodes' ).subscribe();
    this.episodes$ = Episodes.find();
);

This option looks a bit weird, since you setup a subscription, but we don't use a subject (rxjs) and then we just seem to bind Episodes.find(), which is a rxjs mongo observable collection at this time, to the episodes$ observable.

The second method:
This subscription DOESN'T auto update, only loads one time on page load:

export class PodcastSearchComponent implements OnInit {
  episodes$: Observable<Episode[]>;

  constructor(
  ) {}

  ngOnInit() {
    this.episodes$ = MeteorObservable.subscribe( 'episodes' ).pipe( switchMap( () => Episodes.find() ) );

I ran into the second method in one of the examples and it looked elegant, but it has some different behaviour than the first method.
I would have expected this to work since the RxJS Mongo observable collection that was setup. However it doesn't autorun, or detect changes in my situation.

Should I also use the second method for setting up a subscription?

I hope my questions aren't annoying, I am re-exploring Meteor after 2 years of absence (just pure angular 4 during that time) and I really want it to work with Angular 6.

***Update: at one point I thought it had something to do with ChangeDetectionStrategy.OnPush but it didn't matter turning that off.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions