Deleting Recently Added Node Causes Empty Subscribe


#1

BundleCollection = {
_id : “ABC”,
list : [
{ id : 1, name: “John”},
{ id : 2, name: “Bob”},
{ id : 3, name: “Steve”}
]
}

PersonsCollection = {
{ _id : 1, name: “John”, Phone: 123}
{ _id : 2, name: “Bob”, Phone: 456}
{ _id : 3, name: “Steve”, Phone: 789}
{ _id : 4, name: “Christina”, Phone: 301}
}

var bPub = null;
Template.Bundle.onCreated(function() {
self = this;
self.autorun(function() {
bPub = self.subscribe(“bundlePub”, “ABC”);
if( bPub.ready()) {
// Build id list
// ids = array of id from bundle list
self.subscribe(“personPub”, ids);
}
});
});

Template.Bundle.helpers({
list: function() {
return PeronsCollection.find();
},

When I add { id : 4, name: “Christina”} into “ABC” document. The template renders all 4 people. But if I delete Christina right after I added her via click event -> meteor.call, personPub subscription is empty. When I debug, the “list” helper is triggered twice. The fist run, Christina is removed, the second, PersonsCollection.find() return empty.

However, if I add Christina, go to a different route and come back, I can delete Christina without any issue. I don’t understand why “list” helper is triggered twice, and why it return empty list. Thanks in advance.


#2

I’m guessing it may be down to your publications. I’ve just written a small app using (what you supplied of) your code and it works just fine.

EDIT: Pushed to a git repo. HIH:


#3

Apologies, because sensitivity of the project, I had to clean up a lot of the code. Here’s another version of it.

/*** Bundle.js ***/
var bPub = null;

Template.Bundle.onCreated(function() {
  self = this;
  self.autorun(function(){
  	bPub = self.subscribe("bundlePub", FlowRouter.getParam('bundleID'));
  });
  self.autorun(function(){
    if(bPub.ready()) {
      var b= BundleCollection.findOne();
      var ids = [];
      for(var person of b.list) {
        ids.push(person.id);
      }
      self.subscribe("personsByIds", ids);
    }
  });
  self.groupList = function() {
      var list = BundleCollection.findOne().list;
      var persons = PersonsCollection.find({}).fetch();
      return transform(list, persons);
  };
});

function transform(list, persons) {
  var result= [];
  for(var per of persons) {
  	var  found= _.findWhere(list, {id: per.id});
  	if(found) {
          var doc = {
              _id : per.id,
              name: per.name,
              phone: per.phone
             };
         result.push(doc);
     }
  }
  return result;
}

Template.Bundle.helpers({
  list: function() {
  	return Template.instance().groupList();
  },
});

/*** Bundle Template ***/
<template name="Bundle">
  {{#if template.subscriptionsReady}}
     {{> BundleItem list}}
  {{/if}}
</template>

/*** BundleItem Template ***/
<template name="BundleItem">
   <table>
   {{#each list}}
    <tr>
    <td>this.name</td>
    <td>this.phone</td>
    <td><label id="delete">Delete</label></td>
    </tr>
    {{/each}}
   <table>
</template>

/*** BundleItem.js ***/
Template.BundleItem.helpers({
  list: function() {
    return Template.instance().data;
  }
});

// SERVER CODE 
Meteor.publish("bundlePub", function(id) {
  if (SecurityUtil.isActivatedPubCall(this.userId)) {
    var cursor = BundleCollection.find({
        _id: id
    });
    return cursor;
  } else {
    return this.ready();
  }
});

Meteor.publish("personsByIds", function(ids) {
  if(SecurityUtil.isActivatedPubCall(this.userId)) {
    var selector = {
      _id : {$in : ids}
    };
    
    return PersonsCollection.find(selector);
  } else {
    this.ready();
  }
})