Skip to content Skip to sidebar Skip to footer

Meteor Collection Insert Not Updating On Client

I'm currently having an issue with a collection insert in Meteor. I call a method to insert a new item into a collection. The server database shows the new item but the client side

Solution 1:

There could be more to it, but I couldn't see there where you have included the Meteor.subscribe("items") on the client side as that is what will pass the records to the client from the publish on the server.

Also in your publish, you can use

if (this.userId) {
    //do stuff logged in
} else {
    this.stop();
}

instead of selecting the Meteor.user() record for the user ID to detect if the client is logged in or not.

Solution 2:

Your publication:

Meteor.publish("items", function(){
  if(user = Meteor.user()){
    returnItems.find( {household : user.household});
  }
  else{
    this.ready();
  }
});

Your block helper:

Template.itemList.helpers({
  items: function() {
    return Items.find({}, {sort : {checked: 1, createdAt: -1}});
  }
});

Your subscription (client side): Meteor.subscribe("items");

If you are using iron:router. In the route you can define directly your helper and your subscription like this and just call {{items}} in your template:

Router.route('myRoute', function(){
        this.render('myRouteTemplate');
    }, {
    path: '/myRoutePath',
    waitOn: function(){
        if(user = Meteor.user()) {
            var subscriptions = [];
            //// Subscribe to your subscription//===============================
            subscriptions.push(Meteor.subscribe('items'));

            return subscriptions;
        }
    },
    data: function(){
        var context;
        if(this.ready()) {
            if (user = Meteor.user()) {
            context = {
                items: Items.find( {household : user.household})
              };
            }           
        }
    }
    });

Always remember that when you publish a magazine, you need a subscriber process. Same thing for collections.

Cheers,

Solution 3:

I ended up fixing this issue by implementing Iron Router in my application and waiting on subscriptions to be ready before rendering a template. I believe that my issue before was some kind of race condition but i'm not positive on the specifics of it. Either way i'm satisfied with my solution because it fixes my issue and structures the flow of my site better and more understandably I think.

Post a Comment for "Meteor Collection Insert Not Updating On Client"