提问者:小点点

mongoose中的保存功能不工作


我有一个模式,当我试图更新数据库中的数组时,文件没有得到更新,数组总是空的。当我在更新后打印帖子时,它确实打印了带有一个注释的数组,这是我刚刚添加的,但它没有保存在数据库中。我很可能没有将更新后的文件正确保存到数据库中

下面是我的comment_controller

    
const Comment = require('../models/comment');
const Post = require('../models/post');

module.exports.create = function(req, res){
    Post.findById(req.body.postId, (err, post) => {
        //if the post is found
        if(post){
            Comment.create({
                content: req.body.comment,
                user: req.user._id,
                post: req.body.postId
            },
            (err, currcomment) => {
                if(err)
                    console.log(err);
                post.comment.push(currcomment);
                post.markModified('comment');
                post.save(function(err, doc) {
                    if (err) return console.error(err);
                    console.log("Document inserted succussfully!");
                  });
                    console.log(post);
                return res.redirect('/')
            }
            );
        }
        else{
            return res.redirect('/')
        }

    })
};

下面是我的备注模式

const mongoose = require('mongoose');


const commentSchema = new mongoose.Schema(
    {
        content: {
            type: String,
            required: true
        },
        //comment belongs to a user
        user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        post: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Post'
        }
    },

    {timestamps: true}
);

const Comment = mongoose.model('Comment', commentSchema);

module.exports = Comment;
    

共1个答案

匿名用户

希望您使用的是猫鼬5.10.16版本,请将其改为5.10.15。检查这个GitHub问题线程https://GitHub.com/automattic/mongoose/issues/9585#issue-751097126。5.10.16完全损坏。