节点-无法保存(更新)架构

时间:2019-05-05 12:54:36

标签: node.js mongodb express mongoose

我之所以来是因为在用猫鼬更新架构后保存了一个问题

这是我的路线:

    router.post("/reply", auth, async (req, res) => {
      const { index, reply, imageName, avatar } = req.body;
      const user = await User.findById(req.user.id);
      let post = await Image.findOne({ name: imageName });
      let temp = {
        replier: user.pseudo,
        avatar: avatar,
        replies: reply
      };
      post.commentaires[index].replies.push(temp);
      console.log(post.commentaires);
      post.save(err => {
        if (err) console.log(err);
        return res.status(200).json({ poster: post });
      });
    });

如您所见,我正在将一个对象推入数组,当我记录模型时,一切都应该是正确的,当我尝试它时,应该进行前端更新,但它不会保存到数据库中。我试图通过其他路由保存一些其他信息到我的数据库,但是它仍然有效。

编辑:

我的模式:

   const mongoose = require("mongoose");
   const Schema = mongoose.Schema;

   const ImageSchema = new Schema({
     pseudo: {
       type: String,
       required: true
     },       
     name: {
       type: String,
       required: true
     },
     data: {
       width: {
         type: String,
         required: true
       },
       height: {
         type: String,
         required: true
       }
     },
     likes: {
       type: Number,
       default: 0
     },
     likers: {
       type: Array,
       required: true,
       default: []
     },
     disLikes: {
       type: Number,
       default: 0
     },
     disLikers: {
       type: Array,
       required: true,
       default: []
     },
     commentaires: {
       type: Array,
       required: true,
       default: []
     },
     location: {
       type: String,
       required: true
     },
     date: {
       type: Date,
       default: Date.now
     }
   });

   module.exports = Image = mongoose.model("image", ImageSchema);

日志(顶部显示post.commentaires)

   [
     {"commenteur":"halt","avatar":null,"likes":0,"likers": 
      [],"replies":[]}, 
     {"commenteur":"halt","avatar":null,"likes":0,"likers": 
      [],"replies":[]}, 
     {"commenteur":"halt","avatar":null,"likes":0,"likers": 
      [],"replies":[
        {"replier":"halt","avatar":"data:image/png;base64,VERY LONG IMAGEURI","likes":0,"likers":[],"replies":[],"comment":"test"}
    ]

0 个答案:

没有答案