Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongoosejs找到了一个最好的方法并进行了更新_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js mongoosejs找到了一个最好的方法并进行了更新

Node.js mongoosejs找到了一个最好的方法并进行了更新,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想知道是否有最好的方法: /** * Article Schema */ var PostSchema = new Schema({ title: { type: String, required: true, trim: true }, author:{ type: String, required: true, default: 'whisher' },

我想知道是否有最好的方法:

/**
 * Article Schema
 */
var PostSchema = new Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    author:{
        type: String,
        required: true,
        default: 'whisher'
    },
    slug: {
        type: String,
        index: { unique: true }
    },
    body: {
        type: String,
        required: true,
        trim: true
    },
    avatar:{
        type: String,
        required: true
    },
    status: {
        type: String,
        required: true,
        trim: true
    },
    created: {
        type: Date,
        required: true,
        default: Date.now
    },
    published: {
        type: Date,
        required: true
    },
    categories: {
        type: [String]
    },
    tags: {
        type: [String], 
        required: true,
        index: true
    },
    comment: {
        type: Schema.Types.ObjectId, 
        ref: 'CommentSchema'
    },
    meta: {
        votes: {
            type: Number,
            default: 0
        },
        comments: {
            type: Number,
            default: 0
        } 
    }
});
/**
 * Comment Schema
 */
var CommentSchema = new Schema({
    post_id: {
        type: Schema.Types.ObjectId,
        ref: 'Post',
        required: true
    },
    author:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true
    },
    web:{
        type: String
    },
    body: {
        type: String,
        required: true,
        trim: true
    },
    status: {
        type: String,
        required: true,
        default: 'pending'
    },
    created: {
        type: Date,
        required: true,
        default: Date.now
    },
    meta: {
        votes: Number
    }
});
/**
 * Create a comment
 */
exports.create = function(req, res) {
    var comment = new Comment(req.body);
    comment.save(function(err) {
        if (err) {
           return res.jsonp(500,{ error: 'Cannot save the comment' });
        } 
        Post.findById(comment.post_id).exec(function(err, post) {
            if (err) {
               return res.jsonp(404,{ error: 'Failed to load post with id ' + comment.post_id });
            }
            if (!post) {
                return res.jsonp(404,{ error: 'Failed to load post with id ' + comment.post_id });
            }
            post.meta.comments = post.meta.comments++;
            post.save(function(err) {
                if (err) {
                    return res.jsonp(500,{ error: 'Cannot update the post' });
                } 
                res.jsonp(200,comment);
            });
        });
    });
};
顺便说一句,我只是在看

但就像这样:

Model.findByIdAndUpdate(comment.post_id, { post.meta.comments: post.meta.comments++ })

不工作

我认为您需要使用操作符来增加注释计数,如下所示

Post.findByIdAndUpdate(comment.post_id, { $inc: {"meta.comments" : 1} }, callback);

你能确认
post.meta.comments
是在当前范围内定义的吗?这就是问题所在^^有没有办法使用populate?