无法更新,$ push in mongoose,nodejs

时间:2017-07-25 22:16:00

标签: javascript node.js mongodb

我正在尝试在我的博客中添加评论选项,但我正在使用update(),$ push在模式中更新它。我做错了什么?

posts.js->路由

var posts = require('../models/posts');
   router.post('/addcomment', function(req, res, next) {
        // Get form values
        var name        = req.body.name;
        var body        = req.body.body;
        var postid      = req.body.postid;
        var commentdate = new Date();
        console.log('postid:'+postid+'Comment:'+body);
        // Form validation
        req.checkBody('body', 'Body field is required').notEmpty();

        var errors = req.validationErrors();

        if (errors) {
            var posts = db.collection('posts');
            posts.findById(postid, function(err, post) {
                res.render('show', {
                    "errors": errors,
                    "post": post
                });
            });}
        else {
            var comment = {
                "name": name,
                "body": body,
                "commentdate": commentdate
            };
            console.log(comment.name);
            var posts = db.collection('posts');
            posts.update({
                    "_id": postid
                },
                {
                    "$push":{
                        "comments": comment
                    }
                },
                {
                    save: true,
                    new: true
                },
                function(err, doc) {
                    if(err) {
                        console.log(err);               
                    } else {
                        req.flash('success', 'Comment added');
                        res.location('/posts/show/' + postid);
                        res.redirect('/posts/show/' + postid);
                    }
                }
            );
        }
    });

    module.exports = router;

posts.js->模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var lists = new Schema({
    title:{
        type: String,
        required: true
    },
    author_username:{
        type: String
    },
    author_name: {
        type: String
    },
    category:{
        type: String
    },
    body:{
        type: String,
        required: true
    },
    // comments: [CommentsSchema]
    comments: [{
        name:{
            type: String,
        },

        body:{
            type: String,
        },
        parent: {
            type: String
        },
        commentdate :{
            type: Date,
            default: Date.now
        }
    }],
    date:{
        type: Date,
        default: Date.now
    }
});

var posts = module.exports = mongoose.model('posts', lists);

index.jade

if post.comments
                                h3 Comments
                                each comment, i in post.comments
                                    p.comment-name #{comment.name}
                                    p.comment-text #{comment.body}

输出

  

postid:57fgc24d24d5f3hj,评论:'这是评论'。

我尝试过以前在这里提出的问题,但无法理解。 如何添加功能来添加评论评论 感谢

0 个答案:

没有答案