流星-表单提交时没有插入到集合中



我正在尝试使用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/>
      <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()时,那里什么也没有。

我是流星的新手,所以这可能是一个非常愚蠢的问题,但我感谢任何帮助!

您可能需要在调用insert()时使用Meteor.userId()而不是Meteor.user()。如果没有autopublish包,Meteor.user()返回的文档在客户机上可能与在服务器上不同(出于安全原因)。这意味着客户端插入到您的迷你mongodb和服务器端插入到真正的mongodb可能会相互冲突。我希望在服务器端插入的结果传播回客户机之后,忽略客户端插入。我不确定为什么它没有被服务器端插入所取代。当你在服务器上运行LostItems.find().fetch()(例如在meteor shell)时,它会返回什么?

我通过将insecure, yogiben:autoform-tagsautopublish添加到我的包列表来修复这个问题。我认为autopublish是造成这种差异的原因。我相信有更好的方法来做到这一点,这可能有一些安全漏洞,但这不是一个大项目,它不存储敏感数据,所以现在可以工作。

最新更新