无法在 meteorjs 中向 post 添加评论

Unable to add comments to a post in meteorjs

我无法向特定的评论添加评论 post.The 评论没有被插入到 mongo 数据库中。

Comments = new Mongo.Collection('comments');
Template.comments.helpers({
    'comment': function(){
        console.log(this._id);
        return Comments.find();

    }
});
Template.addComment.events({
        'click button':function(event){
            event.preventDefault();
            var madeBy = Meteor.user().username;
            var comment = document.getElementById('mycomment').value;
            var currentPost = this._id;


            Comments.insert({
                comment:comment,
                createdAt:new Date(),

                madeBy:madeBy,

            });
            document.getElementById('mycomment').value='';
        }
    });

评论页的HTML代码为:

<template name="comments">
    <h2><b>{{name}}</b></h2>

    {{> addComment}}
    <ul>
        {{#each comment}}
            <li>{{comment}}</li>
        {{/each}}
    </ul>
</template>


<template name='addComment'>
<input type='text' placeholder='Add comment here' name='comment' id ='mycomment'>
<button class="btn btn" type="button" id='btn'>Comment</button>
</template>

这里的{{name}}指的是被评论的post的名字。 请帮帮我 out.Thankyou.

您应该将表单元素放在 addComment 模板上;

    <template name='addComment'>
    <form class="add-Comment">
    <input type='text' placeholder='Add comment here' name='comment' id ='mycomment'>
    <button class="btn btn" type="button" id='btn'>Comment</button>
    </form> 
    </template>

然后在你的js文件中:

Template.addComment.events({
  'submit .add-Comment': function(event){
   ...  
  return false;
  }
});