【问题标题】:Meteor: Why does my subscription not work?Meteor:为什么我的订阅不起作用?
【发布时间】:2015-07-21 00:39:44
【问题描述】:

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

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 处失败,但有以下异常...

[Log] 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 毫秒超时内准备就绪。我给光标的变量错误了吗?

【问题讨论】:

    标签: meteor publish-subscribe


    【解决方案1】:

    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.subscribe(elemId, function() {
            Session.set(elemId, true);
            console.log("subscribed");
            var end = new Date().getTime();
            console.log(end - start);
    
            children = items.find();
        });
    

    【讨论】:

    • 谢谢!工作。 Meteor 文档或博客从不编写完整的代码。匆忙写下的步骤并没有给出完整的画面。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多