【问题标题】:Unable to add comments to a post in meteorjs无法在meteorjs中为帖子添加评论
【发布时间】:2016-06-29 20:58:28
【问题描述】:

不知何故,我无法将 cmets 添加到特定帖子。 cmets 未插入到 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}} 指的是发表评论的帖子的名称。 请帮帮我。谢谢。

【问题讨论】:

    标签: javascript mongodb meteor meteor-blaze


    【解决方案1】:

    你应该把表单元素放在你的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;
      }
    });
    

    【讨论】:

    • 感谢您的帮助。原来 cmets 不可见,因为我删除了自动发布。仍然非常感谢您的帮助。
    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2019-12-07
    • 2019-12-06
    相关资源
    最近更新 更多