【发布时间】:2015-09-19 03:09:49
【问题描述】:
我正在尝试使用流星存储放入此表单的信息:
<form class="form-group" id="lost_form">
<label for="item_name">Type</label>
<input id="item_name" class="form-control" type="text" placeholder="What is the item? Ex: Water bottle" required/>
<label for="item_brand">Brand</label>
<input id="item_brand" class="form-control" type="text" placeholder="What brand is the item? Ex: Nalgene" required/>
<label for="item_desc">Description</label>
<input id="item_desc" class="form-control" type="text" placeholder="Describe the item. Ex: Green, name on bottom" required/>
<label for="item_loc">Location</label>
<input id="item_loc" class="form-control" type="text" placeholder="Where did you have it last? Ex: Main common room"/>
<label for="item_date">Date Missing</label>
<input id="item_date" class="form-control" type="date"/>
<br>
<input id="submit_lost_form" class="btn btn-primary btn-block" type="submit" value="Submit" />
</form>
我用来将其放入集合的 JS 如下:
LostItems = new Meteor.Collection('lostitems');
Meteor.methods({
'insertItem': function(iname, ibrand, idesc, iloc, idate){
LostItems.insert({
user: Meteor.user(),
name: iname,
brand: ibrand,
description: idesc,
location: iloc,
date: idate
})
}
});
if (Meteor.isClient) {
Template.lost_form.events({
'submit form': function (event) {
event.preventDefault();
var itemName = event.target.item_name.value;
var itemBrand = event.target.item_brand.value;
var itemDesc = event.target.item_desc.value;
var itemLoc = event.target.item_loc.value;
var itemDate = event.target.item_date.value;
Meteor.call('insertItem', itemName, itemBrand, itemDesc, itemLoc, itemDate);
}
});
}
但是每当我提交表单时,什么都没有发生。开发者控制台或流星控制台中没有错误,当我执行LostItems.find().fetch() 时,那里什么也没有。
我是流星新手,所以这可能是一个非常愚蠢的问题,但我感谢任何帮助!
【问题讨论】:
-
这行得通 - 就像您的代码一样,不包括用户字段。 meteorpad.com/pad/JkxBLRq8XLJTpxvnc/Leaderboard 如果用户字段是问题,我认为它不会静默失败,所以我认为您的问题可能在其他地方。
-
在您的示例中,模板的命名方式并不明显。您的表单的 id 为
lost_form,并且您在Template.lost_form上设置了您的事件。您是否也将您的模板命名为lost_form?然后它应该像@JeremyK 所说的那样工作。否则很清楚为什么它不起作用。 -
@Valentin 我的模板也被命名为
lost_form。表单是否应该具有名称属性而不是 ID?那会有什么不同吗? -
不……你甚至不需要ID,因为你没有查询它。提交表单时页面是否重新加载?您也可以检查服务器日志输出。
-
@JeremyK 提出的唯一错误是我有两个名为
lost的模板,但我找不到重复的。会这样吗?
标签: javascript forms mongodb meteor meteor-accounts