Answers for "delete single image in node js mongodb"

0

delete single image in node js mongodb

// Delete single image
const deleteImage = (req, res) => {
  Imageup.deleteOne({ _id: req.params.id })
    .then(() => {
      res.status(200).json({
        message: "Image deleted successfully!",
      });
    })
    .catch((error) => {
      res.status(400).json({
        error: error,
      });
    });
};
Posted by: Guest on April-12-2021
0

delete single image by using user id in node js mongodb

// User delete his single image by using user id and image id
const deleteSingleImageByUserId = async (req, res) => {
  try {
    const user = await User.findByIdAndUpdate(
      req.params.userId,
      {
        $pull: {
          images: {
            _id: req.params.imageId,
          },
        },
      },
      { new: true }
    );

    //console.log("req params imageId: ", req.params.imageId + images);

    if (!user) {
      return res.status(400).send("User not found");
    }

    await Imageup.findByIdAndDelete(req.params.imageId);

    res.send("Image is deleted successfully!");
    console.log("user user: ", user);
  } catch (err) {
    res.status(500).send("Something went wrong!");
  }
};
Posted by: Guest on April-12-2021

Code answers related to "delete single image in node js mongodb"

Code answers related to "Javascript"

Browse Popular Code Answers by Language