猫鼬中的保存功能不起作用

Posted

技术标签:

【中文标题】猫鼬中的保存功能不起作用【英文标题】:The save function in mongoose not working 【发布时间】:2021-03-10 08:34:47 【问题描述】:

我有一个架构,当我尝试更新数据库中的数组时,文件没有得到更新,并且数组始终为空。 当我在更新后打印帖子时,它会打印带有我刚刚添加的一条评论的数组,但它没有保存在数据库中。 很可能我没有将更新的文件正确保存到数据库中

这是我的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('/')
        

    )
;

这是我的 cmets 架构

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;
    

post.save() 给出了这个警告/错误


 Error: Post validation failed: comment: Cast to [undefined] failed for value "["_id":"5fc0dfec4c87ad723b06d6eb","content":"sadfasdf","user":"5fba3cbd01b2310b7aba868b","post":"5fc0de91fc0ca96fd0b331df","createdAt":"2020-11-27T11:15:56.991Z","updatedAt":"2020-11-27T11:15:56.991Z","__v":0]" at path "comment"
    at ValidationError.inspect (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/error/validation.js:47:26)
    at formatValue (internal/util/inspect.js:491:31)
    at inspect (internal/util/inspect.js:189:10)
    at Object.formatWithOptions (util.js:84:12)
    at Console.(anonymous function) (console.js:196:15)
    at Console.warn (console.js:213:31)
    at /home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/controllers/comment_controller.js:19:45
    at /home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/model.js:4846:16
    at /home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/helpers/promiseOrCallback.js:16:11
    at /home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/model.js:4869:21
    at $__save.error (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/model.js:500:16)
    at /home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:246:48
    at next (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:167:27)
    at next (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:169:9)
    at Kareem.execPost (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:217:3)
    at _handleWrapError (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:245:21)
  errors:
    comment:
       ValidatorError: Cast to [undefined] failed for value "["_id":"5fc0dfec4c87ad723b06d6eb","content":"sadfasdf","user":"5fba3cbd01b2310b7aba868b","post":"5fc0de91fc0ca96fd0b331df","createdAt":"2020-11-27T11:15:56.991Z","updatedAt":"2020-11-27T11:15:56.991Z","__v":0]" at path "comment"
          at _init (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/document.js:691:37)
          at init (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/document.js:657:5)
          at model.Document.$__init (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/document.js:586:3)
          at model.syncWrapper [as $__init] (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/kareem/index.js:234:23)
          at model.Document.init (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/document.js:545:8)
          at completeOne (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/query.js:2844:12)
          at model.Query.Query._completeOne (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/query.js:2073:7)
          at Immediate.Query.base.findOne.call (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mongoose/lib/query.js:2117:10)
          at Immediate.<anonymous> (/home/yuganksingh/me/Learn Full stack WEb-Dev/CODING NINJAS/Backend/instaKiloGram/node_modules/mquery/lib/utils.js:116:16)
          at runCallback (timers.js:705:18)
          at tryOnImmediate (timers.js:676:5)
          at processImmediate (timers.js:658:5)
        properties: [Object],
        kind: 'cast',
        path: 'comment',
        value:
         '["_id":"5fc0dfec4c87ad723b06d6eb","content":"sadfasdf","user":"5fba3cbd01b2310b7aba868b","post":"5fc0de91fc0ca96fd0b331df","createdAt":"2020-11-27T11:15:56.991Z","updatedAt":"2020-11-27T11:15:56.991Z","__v":0]',
        reason: [CastError]  ,
  _message: 'Post validation failed' 

完整的代码在这里 https://github.com/YugankSingh/instaKiloGram

【问题讨论】:

【参考方案1】:

希望你使用的是 mongoose 5.10.16 版本,请改成 5.10.15。 检查这个 GitHub 问题线程 https://github.com/Automattic/mongoose/issues/9585#issue-751097126。 5.10.16 彻底坏掉了。

【讨论】:

我检查了 package.json 它的 "mongoose": "^5.10.15", 请不要使用任何波浪号(~)和插入符号(^),只需在 package.json 中执行 "mongoose": "5.10.15" 即可。 出现同样的问题没有变化

以上是关于猫鼬中的保存功能不起作用的主要内容,如果未能解决你的问题,请参考以下文章

扩展模式猫鼬中的“虚拟”继承方法不起作用

护照本地猫鼬中的密码验证器选项不起作用

使用数组过滤器更新多个嵌套数组在猫鼬中不起作用

在猫鼬中排序

如何删除猫鼬中的Many

如何在猫鼬中插入自动增量编号