Добавление комментария к сообщению без обновления страницы (например, Instagram) Node.js, Express, Mongoose - PullRequest
1 голос
/ 05 ноября 2019

Я пытаюсь создать сайт, похожий на Instagram, и пытаюсь добавить комментарий к сообщению, не обновляя всю страницу, я работаю с Node.js, Express, Mongoose. Я новичок и проверил другие подобные вопросы, но не нашел ничего, что охватывало бы эту тему, используя экспресс-фреймворк. Буду очень благодарен за вашу помощь! Некоторые из моего кода:

//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"
        }
     ]
});
...