showing results for - "delete single image in node js mongodb"
Jana
04 Oct 2016
1// Delete single image
2const deleteImage = (req, res) => {
3  Imageup.deleteOne({ _id: req.params.id })
4    .then(() => {
5      res.status(200).json({
6        message: "Image deleted successfully!",
7      });
8    })
9    .catch((error) => {
10      res.status(400).json({
11        error: error,
12      });
13    });
14};
Gage
14 Apr 2019
1// User delete his single image by using user id and image id
2const deleteSingleImageByUserId = async (req, res) => {
3  try {
4    const user = await User.findByIdAndUpdate(
5      req.params.userId,
6      {
7        $pull: {
8          images: {
9            _id: req.params.imageId,
10          },
11        },
12      },
13      { new: true }
14    );
15
16    //console.log("req params imageId: ", req.params.imageId + images);
17
18    if (!user) {
19      return res.status(400).send("User not found");
20    }
21
22    await Imageup.findByIdAndDelete(req.params.imageId);
23
24    res.send("Image is deleted successfully!");
25    console.log("user user: ", user);
26  } catch (err) {
27    res.status(500).send("Something went wrong!");
28  }
29};