1

I have following on server:

items.allow({
'insert': function (userId,doc) {
  return true; 
}
});


Meteor.methods({
getChildren: function(parentId) {
                var children = items.find({parent: parentId});
                console.log("children: "+children.count());
                Meteor.publish(parentId, function() {
                    console.log("publishing to : "+parentId);
                    return children;
                });
                return true;
}
});

Following on collections.js available for both server and client...

items = new Mongo.Collection("folders");

Next up, Client has following:

Meteor.startup(function() {
    items.insert({name: "HelpDocs", parent: "DocumentsA"});
    items.insert({name: "Code", parent: "DocumentsA"});
    items.insert({name: "Unit Tests", parent: "DocumentsA"});
});

Template.fileTree.events({
  'click .mainfolders': function (e, t) {
    var elemId = e.currentTarget.id;
    var children = null;
    Meteor.call('getChildren',elemId, function(error, result){
        console.log("subscribing");
        var start = new Date().getTime();
        children = Meteor.subscribe(elemId, function() {
            Session.set(elemId, true);
            console.log("subscribed");
            var end = new Date().getTime();
            console.log(end - start);
        });
        Meteor.setTimeout(function() {
               children.forEach(function(child) {
                    console.log("rendering");
                    Blaze.render(Template.fileTree, $('#'+elemId).get(0));
                });
            Meteor.stop(elemId);
        }, 800);
    });
  }
});

It fails at children.forEach with following exception...

[Log] Exception in setTimeout callback: http://0.0.0.0:3000/Site/client/filetree.js?83d0c4ebb8e92bb0e03e82f52ea4c0510cc0d831:21:35 (meteor.js, line 888) withValue@http://0.0.0.0:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:955:21 http://0.0.0.0:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:435:54 http://0.0.0.0:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:983:27

The subscription is ready well within the 800 milliseconds timeout i set. Am i giving the variable wrong for cursor?

Ram
  • 325
  • 4
  • 22

1 Answers1

1

Meteor.subscribe() does not return a cursor. Per the docs, it "Returns a handle that provides stop() and ready() methods."

Instead of :

    children = Meteor.subscribe(elemId, function() {
        Session.set(elemId, true);
        console.log("subscribed");
        var end = new Date().getTime();
        console.log(end - start);
    });

You might try something like (untested):

    Meteor.subscribe(elemId, function() {
        Session.set(elemId, true);
        console.log("subscribed");
        var end = new Date().getTime();
        console.log(end - start);

        children = items.find();
    });
bluebird
  • 640
  • 4
  • 9
  • Thanks! worked. Meteor documentation or blogs never write full code. The hastily written steps do not give full picture. many thanks! – Ram Jul 21 '15 at 01:45