简体   繁体   English

Meteor 删除 function 不适用于集合

[英]Meteor remove function doesn't work on a Collection

I'm practicing with Meteor and I can't understand why the remove function of a MongoDB collection doesn't work.我正在练习 Meteor 并且我不明白为什么删除 MongoDB 集合的 function 不起作用。 This is my html template where I created a button to remove the news:这是我的 html 模板,我在其中创建了一个按钮来删除新闻:

<template name="news">
  <h2>News</h2>
  <form>
    <input type="text" name="title" placeholder="Add some news">
    <button>Add</button>
  </form>
  <ul>
  {{#each News in showNews}}
  <li>{{News.title}}<button class="removeAction">&times;</button></li>
  {{/each}}
</ul>
</template>

And this is my client.js file where I created the event to execute the remove function:这是我的 client.js 文件,我在其中创建了执行删除 function 的事件:

Template.news.events({
  'submit form': function(e) {
    e.preventDefault();
    var title = e.target.title.value;
    News.insert({ title: title, createdAt: new Date() });

    e.target.title.value = "";
  },
  'click .removeAction': function(e, a) {
    News.remove(this._id);
  }
});

I can't really understand why it doesn't work, I tried a lot of changes but nothing worked, when I click the "X" button nothing is happening.我真的不明白为什么它不起作用,我尝试了很多更改但没有任何效果,当我单击“X”按钮时没有任何反应。

Most probably the issue is that this._id is not defined.最有可能的问题是 this._id 没有定义。 I think you are not using blaze the correct way.我认为您没有以正确的方式使用 blaze。 I'm not a blaze user but I think this will work.我不是 blaze 用户,但我认为这会起作用。

<template name="news">
  <h2>News</h2>

  <form>
    <input type="text" name="title" placeholder="Add some news">
    <button>Add</button>
  </form>

  <ul>
  {{#each showNews}}
    {{> newsPost}}
  {{/each}}
  </ul>
</template>

<template name="newsPost">
  <li>{{title}}<button class="removeAction">&times;</button></li>
</template>

And your removeAction event listener should look like this.你的 removeAction 事件监听器应该是这样的。

Template.newsPost.events({
  'click .removeAction': function(e, a) {
    News.remove(this._id);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM