Javascript Meteor-提交时未插入收藏的表单
我正在尝试使用meteor存储此表单中的信息:Javascript Meteor-提交时未插入收藏的表单,javascript,forms,mongodb,meteor,meteor-accounts,Javascript,Forms,Mongodb,Meteor,Meteor Accounts,我正在尝试使用meteor存储此表单中的信息: <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/>
<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>
但每当我提交表格时,什么都没有发生。开发者控制台或meteor控制台中没有错误,当我执行LostItems.find().fetch()时,那里什么都没有
我是新来的流星,所以这可能是一个非常愚蠢的问题,但我感谢任何帮助 我通过将unsecure
、yogiben:autoform标签
和autopublish
添加到我的包列表中,解决了这个问题。我认为,autopublish
是造成差异的原因。我确信有更好的方法可以做到这一点,而且这可能有一些安全缺陷,但这不是一个大项目,而且它没有存储敏感数据,所以现在就可以了。在调用插入()
时,您可能需要使用Meteor.userId()
而不是Meteor.user()
。如果没有autopublish
软件包,那么Meteor.user()
返回的文档在客户端上可能与服务器上不同(出于安全原因)。这意味着,迷你mongodb中的客户端插入和真正mongodb中的服务器端插入可能会相互冲突。我希望在服务器端插入的结果传播回客户端之后,客户端插入会被忽略。但我不确定为什么它没有被服务器端插件所取代。当您在服务器上运行它时,LostItems.find().fetch()
返回什么(例如在meteor shell
中)?这是有效的-与您的代码一样,不包括用户字段。如果用户字段是问题所在,我不认为它会自动失败,因此我认为您的问题可能在其他地方。在您的示例中,您的模板的命名方式并不明显。您的表单id为lost\u form
,您可以在Template.lost\u form
上设置事件。您是否也将模板命名为“丢失的表单”
?那么它应该像@JeremyK所说的那样工作。否则,很明显它为什么不工作。@Valentin我的模板也被命名为lost\u form
。表单是否应该具有名称属性而不是ID?这会有区别吗?不…你甚至不需要ID,因为你不需要查询它。提交表单时是否重新加载页面?您还可以检查服务器日志输出。@JeremyK引发的唯一错误是,我有两个名为的模板丢失了
,但我找不到重复的模板。这样行吗?看看下面迪安·布雷特尔的答案。它允许您删除自动发布包,并解释了为什么在还原为0之前,丢失的\u计数会短暂显示为1。这称为延迟压缩。
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);
}
});
}