【发布时间】: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