mongodb - Subscription not updated when collection subfield changes in publication (using $slice) -


i have publication receives parameter subscription determine how many records return array inside collection (using $slice). issue have data not refreshed on template when new parameter sent through subscription. code:

publication:

meteor.publish('conversations', function (record_limit) {   var bunchsize = 50;   var multiplier = 1;   if (record_limit) {     multiplier = record_limit;   }    var publimit = bunchsize * multiplier;   return conversations.find({users:{$in:[this.userid]}},{fields:{"lstmsg":1,  "msg":{"$slice":-publimit}}});         }); 

trigger load more array objects:

var convcounter = 1; template.conversation.events({   'click .js-loadmore': function() {     convcounter = convcounter + 1;     deps.autorun(function() { meteor.subscribe("conversations",convcounter); });   } }); 

any advice why subscription not being refreshed on template? shows 50 items "msg" field while has more 200 items on it. in advance.

try using reactivevar instead , wrapping subscription in autorun inside template's oncreated callback.

template.conversation.oncreated(function() {   this.convcounter = new reactivevar(1);    this.autorun(() => {     this.subscribe('conversations', this.convcounter.get());   }); });  template.conversation.events({   'click .js-loadmore': function(event, instance) {     instance.convcounter.set(instance.convcounter.get() + 1);   } }); 

as process click event update reactivevar in tern cause previous publication stop , start new 1 (with new $slice value).

you have modify publication , use lower level api in order trick mergebox send down sub-field changes client (otherwise, if re-subscribe new record_limit value client record not updated msg sub-field values). note, approach viable if subscribing client uses data in "read only" fashion because modify _id field being sent down in order trick mergebox.

meteor.publish('conversations', function (record_limit) {   var bunchsize = 50;   var multiplier = 1;   if (record_limit) {     multiplier = record_limit;   }    var publimit = bunchsize * multiplier;    const handle = conversations.find({     users: {       $in: [this.userid]     }   }, {     fields: {       "lstmsg": 1, "msg": {         "$slice": -publimit       }     }   }).observe({     added: (doc) => {       this.added("conversations", doc._id + publimit, doc);     },     changed: (newdoc, olddoc) => {       this.changed("conversations", newdoc._id + publimit, newdoc);     },     removed: (olddoc) => {       this.removed("conversations", olddoc._id + publimit);     },   });    this.ready();    this.onstop(() => {     handle.stop();   });   }); 

the theory upon re-subscription new record_limit argument, data loaded on client (from previous subscription) thrown away , replaced new data because doc _id field different (since have used record_limit part of id).


Comments