首页 > 解决方案 > Mongo在尝试保存数组时返回[object Object]

问题描述

我正在尝试在数组中保存某种评论。该数组有两个值,用户名和评论本身,但是当我尝试保存时,它总是返回 [object Object]。

我尝试的第一种方法是保存评论

// Add the new comment to the blog post's array
                    lesson.comments.push({
                      comment: req.body.comment, // Comment field
                      commentator: user.username // Person who commented
                    });

如果有问题

// Save blog post
                    lesson.save((err) => {
                      // Check if error was found
                      if (err) {
                        res.json({ success: false, message: err }); // Return error message
                      } else {
                        res.json({ success: true, message: 'Comment saved' }); // Return success message
                      }
                    });

但这没有用

然后我尝试改变方式findByIdAndUpdate

Lesson.findByIdAndUpdate(req.body.id,
                   {
                     $push: {
                     "comments.comment":req.body.comment,
                     "comments.commentator": req.body.id
                   }
                   },
                   {
                     new:true
                   },
                   function(err,course){
                     if(err){
                       res.json({success:false, message:err});
                       console.log(req);
                     }else{

                       res.json({success:true, message:req.body.comment});
                       //res.json({user});
                     }
                   }
                 )
                 }

但我找不到问题

一切看起来都很好

标签: node.jsmongodbmongoosemean-stack

解决方案


推荐阅读