gpt4 book ai didi

javascript - 无法在meteorjs 中的帖子中添加评论

转载 作者:行者123 更新时间:2023-12-03 06:38:02 25 4
gpt4 key购买 nike

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

最佳答案

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

关于javascript - 无法在meteorjs 中的帖子中添加评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38099703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com