Meteor - 无法在订阅中传递变量

时间:2015-02-26 04:37:16

标签: javascript meteor

我遇到了Meteor的订阅问题。我的目标是从用户输入表单中搜索我的收集标准并吐出匹配的文档。当我硬编码时,我的代码可以使用Meteor.subscribe(' byProductAndBrand',1,2)或任何数字而不是日期和月份。我从网站上尝试了相同数字的日期和月份,并且它没有返回任何结果。我的变量似乎从html表单中获取值,因为它们在控制台中打印,但由于某种原因,它们没有在订阅中传递。任何建议?

ScheduleList集合只是一堆具有特定小时,日和月的文档。

在客户端:

Template.myform.events({
    'click #submit' : function(event, template){
        event.preventDefault();
        Session.set('day', template.find('#day').value);
        Session.set('month', template.find('#month').value);
        var day = Session.get('day');
        console.log(day);
        var month = Session.get('month');
        console.log(month);
        var instance = Template.instance();
        if(instance.byProductAndBrandHandle != null){
            instance.byProductAndBrandHandle.stop();
        }
        instance.byProductAndBrandHandle = Meteor.subscribe('byProductAndBrand',day, month);
    }
});

在服务器中:

Meteor.publish('byProductAndBrand', function(day, month){
    var d = day;
    var m = month;
    return ScheduleList.find({day:d},{month: m});
});

2 个答案:

答案 0 :(得分:1)

服务器发布中的查询限制不正确,应该是:

ScheduleList.find({day:d, month: m});

答案 1 :(得分:1)

顺便提一下,调试此类问题的一种简单方法是在 Meteor.publish 函数中放置调试器; 语句,然后:

$ meteor debug
从控制台

。然后在浏览器中启动 Node Inspector

http://localhost:8080/debug?port=5858

然后您可以验证参数解析并以交互方式仔细检查您的逻辑。桑德的答案是正确的。