3

I have two models:

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

var ProjectSchema = new Schema({
  title: { type: String },
  images: [{
    type: Schema.Types.ObjectId,
    ref: 'Image'
  }]
});
module.exports = mongoose.model('Project', ProjectSchema);

and

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

var ImageSchema = new Schema({
  fileName: { type: String },
  fileSize: { type: Number }
});
module.exports = mongoose.model('Image', ImageSchema);

Existing projects are filled with images as follows:

Project.findById(req.params.project_id, function(err, project) {
  if (err) { res.status(400).send(err); }

  var image = new Image({
    fileName: req.file.name,
    fileSize: req.file.size
  });

  image.save(function(err) {
    if (err) { res.status(400).send(err); }

    project.images.push(image);
    project.save();
  );
});

There are no problems in getting images from the project:

Project.findById(req.params.project_id)
  .populate('images')
  .exec(function(err, project) {
    if (err) { res.status(400).send(err); }

    res.status(200).json(project.images);
  });

i try removing an image from a story, using Mongoose documentation: http://mongoosejs.com/docs/subdocs.html http://mongoosejs.com/docs/api.html#types_documentarray_MongooseDocumentArray.id

Project
  .findById(req.params.project_id)
  .populate('images')
  .exec(function(err, project) {
    if (err) { res.status(400).send(err); }

    project.images.id(req.params.image_id).remove();
    project.save();
  });

But i keep getting errors:

/api-server/app/admin/images/index.js:170
          project.images.id(req.params.image_id).remove();
                         ^
TypeError: project.images.id is not a function

I searched here for solutions, but i only got some things on $pull from 2013. Is the .id() method broken, or am i doing something wrong. As i'm fairly new to mongoose, are there ways to do this better?

Jordy Bulten
  • 155
  • 1
  • 1
  • 9

2 Answers2

0

You just need to delete the image from the database. I hope the following code helps you.

Project
  .findById(req.params.project_id)
  .exec(function(err, project) {
    if (err) { res.status(400).send(err); }
    project.save();
    Image.remove({"_id":project.images._id},function(){})
  });
Charles
  • 1,121
  • 19
  • 30
KOO
  • 61
  • 6
  • btw (can not comment at your deleted question), [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) has a build in map method. it is the second parameter. – Nina Scholz Aug 10 '19 at 14:29
0

You can delete subdocuments by using findByIdAndUpdate and $pull. Seting options to {new: true} overwrites the existing document

var fieldsToRemove= {
      $pull: {
        images: {
          _id: req.params.type
        }
      }
    };

var options = { new: true };

    Project.findByIdAndUpdate(req.params.project_id, fieldsToRemove, options,
     function(err, project) {...

it will remove the subdocument with specified _id

ASem
  • 142
  • 3
  • 16