Paul McLoughlin
Paul McLoughlin

Reputation: 2289

Template which displays collection does not update automatically after data insertion

After inserting data into a collection the template which displays the collection only shows the newly added data after a refresh. I looked into reactive variables but I am having difficulty understanding how I would use them in this context. Any insight would be greatly appreciated.

My code is as follows.

Template Helper

I make a call to get my orders depending on which template has been selected, I store the results in Session. I return the session variable to be used in the template

Template.ordersTable.helpers({
orders : function () {
    var orderType = Session.get('currentOrderTemplate');
    var currentUserId = Meteor.userId();

    if(orderType == 3) {
        // Return all orders created by logged in user.
        Meteor.call('getOrders', {}, function(err, data) {
            if(err)
                console.log(err);
            Session.set('orders', data);
        });
    } else {
        Meteor.call('getOrders', {orderType : orderType}, function(err, data) {
            if(err)
                console.log(err);
            Session.set('orders', data);
        });
    }
    return Session.get('orders');
}
});

Methods

Meteor.methods({
'addOrder' : function(order) {
    OrderList.insert(order);
    check(order, Object);
},
'getOrders' : function (filter) {
    check(filter, Object);
    return OrderList.find(filter).fetch();
}
});

Any other information needed, let me know.

Thanks.

Upvotes: 0

Views: 39

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21374

Method calls are not a reactive data source. Also, you are making your live unnecessarily hard. Just do the find directly on the client:

Template.ordersTable.helpers({
    orders : function () {
        var orderType = Session.get('currentOrderTemplate');
        var currentUserId = Meteor.userId();       
        if (orderType == 3) {
            // Return all orders created by logged in user.
            return OrderList.find({});
        } else {
            return OrderList.find({orderType : orderType});
        }
    }
});

Just make sure you have subscribed to the OrderList collection.

Upvotes: 1

Related Questions