Node - can't save (update) schema

Asked by At

I come because of an issue saving after updating a schema with mongoose

Here's my route:

    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 });
      });
    });

As you can see i'm pushing an object to an array, when i log my model everything is good as it should be, when i try it my front-end updates as it should but it does not save to my DB. I've tried to save some other things though other routes to my DB, it works.

EDIT:

My schema:

   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);

Log (post.commentaires as seen at the top)

   [
     {"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 Answers