There is a possibility that some "news documents" (from one collection) may contain an image (from another collection I'm using cfs:standard-packages
and cfs:filesystem
for file handling).
Here is an example of a news document in the Mongo Database:
{
"_id" : "75d5nZGitsLvo5APM",
"title" : "News with Image",
"description" : "This news entry includes an image",
"type" : "news",
"createdAt" : ISODate("2016-01-31T15:46:01.334Z"),
"coverImageId" : "js5k88PuPeKJwRqcq"
}
And here is an image document:
{
"_id" : "js5k88PuPeKJwRqcq",
"original" : {
"name" : "amir_rahbaran.jpg",
"updatedAt" : ISODate("2014-02-14T14:56:36.000Z"),
"size" : 41614,
"type" : "image/jpeg"
},
"uploadedAt" : ISODate("2016-01-31T15:45:58.060Z"),
"copies" : {
"images" : {
"name" : "amir_rahbaran.jpg",
"type" : "image/jpeg",
"size" : 41614,
"key" : "images-js5k88PuPeKJwRqcq-amir_rahbaran.jpg",
"updatedAt" : ISODate("2016-01-31T15:45:58.000Z"),
"createdAt" : ISODate("2016-01-31T15:45:58.000Z")
}
}
}
The issue at hand: When an admin deletes a news entry, only the news document gets deleted while the images remain in the database. The coverImageId
in the news document and the _id
in the image document are both identical.
This is my template.js code:
Template.adminNewsEventsList.events({
'click #js-delete-ne': function (evt,template) {
evt.preventDefault();
var deleteConfirmation = confirm('Are you sure you want to delete this entry?');
if (deleteConfirmation) {
Images.remove({_id: "coverImageId"});
NewsEvents.remove(this._id);
};
}
});
And this is my template.html code:
<template name="adminNewsEventsList">
<div class="container">
<div class="col-md-6 col-md-offset-3">
{{#each newsEventsData}}
<ul class="list-group">
<li class="list-group-item">
<a title="Edit" id="js-edit-ne" href="{{pathFor route='adminNewsEventsEdit'}}">{{title}}</a>
<!-- edit button -->
<a title="Edit" id="js-edit-ne" class="btn btn-primary" href="{{pathFor route='adminNewsEventsEdit'}}"><i class="fa fa-pencil-square-o"></i></a>
<!-- delete button -->
<a title="Edit" id="js-delete-ne" class="btn btn-danger" href="#"><i class="fa fa-trash"></i> </a>
</li>
</ul>
{{/each}}
</div>
</div>
</template>
Any assistance on this matter would be greatly appreciated.