meteorangular2-meteor

how to use zone() on Meteor.users.find({})


Now in angular2-meteor we started using MongoObservable.Collection in place of Mongo.Cursor and zone() method which helps in collection changes into our view using our Component's Zone and | async in html template. here is link of latest tutorial

Now i am trying to use this zone() method on my Meteor.users.find({}) method to automatically display all the users in my app when any new user created successfully.

Code on my server side

Meteor.publish("userData", function() {
    if (Roles.userIsInRole(this.userId, 'admin')) {
        return Meteor.users.find();
    } else {
        const selector = {
            '_id': this.userId
        };
        return Meteor.users.find(selector);
    }
});

and on client side i have used

userlist: Observable<any[]>;
userSData: Subscription;
     ngOnInit() {
            this.usersData = MeteorObservable.subscribe('userData').subscribe(() => {
                this.userlist=Meteor.users.find({}).fetch();
            });

html code is

   <li class="list-item" *ngFor="let user of userlist">

when i apply .zone() to this this.userlist = Meteor.users.find({}).zone();
i get this error.

TypeError: meteor_1.Meteor.users.find(...).zone is not a function

if i don't use zone() and | async then i get all the user list but if i delete any user or create any new user my list is not automatically updating i have to refresh. For automatic rendering new content we have to use zone and async but its not working with Meteor.users.find().


Solution

  • Maybe the view doesn't update ... try to use NgZone (import it from @angular/core) and use it in your subscription, like this:

    constructor(private ngZone: NgZone) {}
    
    ngOnInit() {
        this.clientsSub = MeteorObservable.subscribe('myClients').subscribe(() => {
            this.ngZone.run(() => {
                this.clients = Clients.find({}, {sort: {name: 1}});
            });
        });
    }