我正在尝试创建一个类似Instagram的网站,并试图在不刷新整个页面的情况下为帖子添加评论,我正在使用Node.js,Express,Mongoose。我还很新,我检查了其他类似的问题,但是还没有使用express框架找到任何涉及此主题的内容。 我将非常感谢您的帮助! 我的一些代码:
//Sending all the posts and their comments to the homepage
app.get("/",async function(req,res){
//get all posts from db
//this also somehow includes all the comments
const allPosts = await posts.find({}).populate('comments');
res.render("home",{posts: allPosts});
});
//comments being displayed under the image
<div class="container">
<h5 class="card-title"></h5>
<p class="card-text"><%= posts.description %></p>
<% posts.comments.forEach(function(posts){ %>
<p><strong><%= posts.comment.author %> - </strong><%= posts.comment.text %></p>
<% }) %>
</div>
//post Schema
var postSchema = new mongoose.Schema({
image: String,description: String,comments: [
{
type: mongoose.Schema.Types.ObjectId,ref: "Comment"
}
]
});