help icon indicating copy to clipboard operation
help copied to clipboard

How to delete data from child and also its image data on server if we delete parent in nodejs mongodb

Open naila62452 opened this issue 3 years ago • 0 comments

Details

I have a database in which the user is a parent and it has some child documents child document has image data too and those images are stored in the AWS s3 bucket. I used MongoDB middleware remove to perform cascade delete. If I delete parents then the data from the child table is also deleted but the image data remains in the s3 bucket. How can I implement the logic that image data should also be deleted from the server on deleting the parent? I also wrote AWS SDK delete APIs but how can I connect them to the parent document?

// This is the parent delete API

  function user_delete(req, res, next) {
      User.findOne({ _id: req.params.id })
         .then(user => {
           if (!user) {
            return next('The user you requested could not be found.')
            }
        Child.remove({ userId: user._id }).exec();
        user.remove();
        return res.status(200).send('User deleted');
    }).catch(err => {
        console.log(err)
        if (err.kind === 'ObjectId') {
            return next(res.status(404).send({
                success: false,
                message: "User not found with id "
            }));
        }
        return next(res.status(500).send({
            success: false,
            message: "Error retrieving User with id "
        }));
    });

}; router.delete('/delete/:id', user_delete); // Delete function for aws SDK delete a file from s3

  function deleteFileStream(fileKey, next) {
       const deleteParams = {
       Key: fileKey,
       Bucket: bucket_name,
   }
 s3.deleteObject(deleteParams, (error, data) => {
        next(error, data)
  })

} exports.deleteFileStream = deleteFileStream;

// Child delete document API

function delete_child(req, res, next) { Child.findById(req.params.id) .then(child => { if (!child) { return next(res.status(404).send({ success: false, message: "child not found with id " + req.params.id })); } // deleting the images of questions also if it has image if(question.file !== '') { const url_parts = url.parse(question.file, true); const datas = url_parts.pathname.split('getImage/') const filekey = datas.pop(); console.log(filekey); deleteFileStream(filekey); // calling the delete function } child.remove() return res.send({ success: true, message: "child successfully deleted!" }); }).catch(err => { if (err.kind === 'ObjectId' || err.name === 'NotFound') { return res.status(404).send({ success: false, message: "child not found with id " + req.params.id }); } return res.status(500).send({ success: false, message: "Could not delete question with id " + req.params.id }); }); } router.delete('/delete/:id', delete_child);

If I call the child API the image is also deleted from the server as I am deleting it but if I delete the parent the child is deleted but not the image. Can anybody tell me, please? I am struggling with this use case.

Node.js version

Not applicable.

Example code

No response

Operating system

Windows

Scope

implementing cascade delete in mongodb

Module and version

Not applicable.

naila62452 avatar Jul 04 '22 23:07 naila62452