0

I need to push an object into an array into the database, but I get an error. What am I doing wrong ? I found such a topic Push items into mongo array via mongoose but can not properly apply ..................................................................................................................................

habalka.files.push({_id: "tata", destination: "Harry"});
    habalka.save((res) => {
      console.log(res)
    });

when I try this code i get this answer

MongooseError: document must have an _id before saving

enter image description here enter image description here

database

{
 "_id": "78935e75-86c0-47c2-a76d-2b52a0a71e7c",
    "files": [
        {
            "_id": "67e848e5-7fb0-480b-a1d3-f3a09d1b57f7",
            "destination": "./uploads/78935e75-86c0-47c2-a76d-2b52a0a71e7c",
            "filename": "67e848e5-7fb0-480b-a1d3-f3a09d1b57f7.mp3",
            "path": "uploads\\78935e75-86c0-47c2-a76d-2b52a0a71e7c\\67e848e5-7fb0-480b-a1d3-f3a09d1b57f7.mp3",
            "folder": "78935e75-86c0-47c2-a76d-2b52a0a71e7c",
            "info": {
                "size": 12120000,
                "mimetype": "audio/mp3",
                "encoding": "7bit",
                "originalname": "Попрошайка Евгений Владимирович из ФАСП. Выпуск 10.mp3",
                "fieldname": "selectedFile"
            },
            "date": {
                "$date": "2019-05-23T01:07:27.122Z"
            }
        }
    ],
    "date": {
        "$date": "2019-05-23T01:07:27.123Z"
    },
    "__v": 0
}

Schema

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

const HabalkaSchema = new Schema({
  _id: {
    type: String
  },
  files: [
    {
      _id: {
        type: String
      },
      destination: {
        type: String
      },
      filename: {
        type: String
      },
      path: {
        type: String
      },
      folder: {
        type: String
      },
      info: {
        size: {
          type: Number
        },
        mimetype: {
          type: String
        },
        encoding: {
          type: String
        },
        originalname: {
          type: String
        },
        fieldname: {
          type: String
        },
      },
      date: {
        type: Date,
        default: Date.now
      }
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Habalka = mongoose.model('habalka', HabalkaSchema);

request

let Habalka = habalka.findOne({_id: "78935e75-86c0-47c2-a76d-2b52a0a71e7c"});
Habalka.files.push({destination: "Harry"});
Habalka.save();
Стас Рябцев
  • 1,318
  • 4
  • 19
  • 36

2 Answers2

0

I believe your issue is pushing to destination when it should be files

I would try

habalka.update(
{_id: "78935e75-86c0-47c2-a76d-2b52a0a71e7c"},
{$push: { files: {destination: "Harry"} }
);
Katherine R
  • 1,107
  • 10
  • 20
  • let Habalka = habalka.findOne({_id: "78935e75-86c0-47c2-a76d-2b52a0a71e7c"}); Habalka.files.push({files: {destination: 'tata'}}); Habalka.save(); I have same problem – Стас Рябцев May 27 '19 at 21:12
  • habalka.update( {_id: "78935e75-86c0-47c2-a76d-2b52a0a71e7c"}, {$push: { files: {destination: "Harry"} } ); No errors occurred with this code, but nothing was written to the database. – Стас Рябцев May 27 '19 at 21:15
  • are you awaiting it or doing a callback? maybe you can show the nodejs code actually making the update – Katherine R May 27 '19 at 21:16
  • router.post('/post', upload.single('selectedFile'), (req, res) => { let habalka = new Habalka(); // 78935e75-86c0-47c2-a76d-2b52a0a71e7c habalka.update( {_id: "78935e75-86c0-47c2-a76d-2b52a0a71e7c"}, {$push: {files: {destination: "Harry"}}} ); }) – Стас Рябцев May 27 '19 at 21:19
0

I found this solution and this work.

Habalka.findById("78935e75-86c0-47c2-a76d-2b52a0a71e7c")
  .then(post => {
    console.log(post)
    post.files.unshift({_id: "tata", destination: "vava"});
    post.save().then(res => console.log(res));
  })
  .catch(err => res.status(404).json({ postnotfound: 'No post found' }));
Стас Рябцев
  • 1,318
  • 4
  • 19
  • 36