Meteor:为什么我的订阅不起作用?

时间:2015-07-21 00:39:45

标签: meteor publish-subscribe

我在服务器上关注:

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;
}
});

关于可用于服务器和客户端的collections.js ......

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

接下来,客户有以下内容:

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);
    });
  }
});

它在children.forEach失败,但有以下异常......

[日志] setTimeout回调中的异常:http://0.0.0.0:3000/Site/client/filetree.js?83d0c4ebb8e92bb0e03e82f52ea4c0510cc0d831:21:35(meteor.js,第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

订阅已在我设置的800毫秒超时内完成。我给光标的变量错了吗?

1 个答案:

答案 0 :(得分:1)

Meteor.subscribe()不会返回游标。根据文档,它"返回一个提供stop()和ready()方法的句柄。"

而不是:

def index
    @agendas = Agenda.all
end

您可以尝试类似(未经测试)的内容:

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