Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星:为什么我的订阅不起作用?_Meteor_Publish Subscribe - Fatal编程技术网

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

Meteor 流星:为什么我的订阅不起作用?,meteor,publish-subscribe,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.c

我在服务器上有以下内容:

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回调中的[Log]异常:(meteor.js,第888行) 有价值@

订阅在我设置的800毫秒超时内准备就绪。是否为游标指定了错误的变量?

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

而不是:

    children = Meteor.subscribe(elemId, function() {
        Session.set(elemId, true);
        console.log("subscribed");
        var end = new Date().getTime();
        console.log(end - start);
    });
您可以尝试以下方法(未经测试):


谢谢工作。Meteor文档或博客从不编写完整的代码。匆忙写下的步骤并不能提供完整的画面。非常感谢!
    Meteor.subscribe(elemId, function() {
        Session.set(elemId, true);
        console.log("subscribed");
        var end = new Date().getTime();
        console.log(end - start);

        children = items.find();
    });