delete single image by using user id in node js mongodb

Solutions on MaxInterview for delete single image by using user id in node js mongodb by the best coders in the world

showing results for - "delete single image by using user id in node js mongodb"
Bailey
24 May 2017
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};
Nadene
15 Jul 2019
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};