【问题标题】:Meteor - can't pass variables in subscribeMeteor - 无法在订阅中传递变量
【发布时间】:2015-04-28 08:44:49
【问题描述】:

我在订阅 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});
});

【问题讨论】:

  • 您的数据库是什么样的?您硬编码的日期是什么?你的输入代码是什么样的?
  • template.find('#day').value 输出很可能是一个字符串,但数据库中的日期和月份字段是数字。因此,您需要先将字符串转换为数字。还有ScheduleList.find({day:d},{month: m})不正确,你要ScheduleList.find({day:d, month: m})
  • 哟,你太棒了,非常感谢!添加了一个 parseInt,现在它可以工作了。这么菜鸟的失误,唉……

标签: javascript meteor


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    $ meteor debug
    

    从控制台。然后在浏览器中启动 Node Inspector

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2015-12-20
      • 2017-10-25
      • 2021-05-20
      相关资源
      最近更新 更多