【问题标题】:Meteor - Form not being inserted into collection when submittedMeteor - 提交时表单未插入到集合中
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我通过将insecureyogiben:autoform-tagsautopublish 添加到我的包列表来解决此问题。我认为autopublish 是与众不同的那个。我确信有更好的方法可以做到这一点,并且这可能存在一些安全漏洞,但这不是一个大项目,它不存储敏感数据,所以现在可以使用。

    【讨论】:

    • 看看下面 Dean Brettle 的回答。它允许您删除自动发布包,并解释了为什么您看到 lost_count 在恢复为 0 之前短暂显示 1。这称为延迟压缩。 discovermeteor.com/blog/latency-compensation
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2016-09-09
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2015-08-20
    相关资源
    最近更新 更多