📌  相关文章
📜  使用节点 js mongodb 中的用户 ID 删除单个图像 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:19.835000             🧑  作者: Mango

代码示例1
// 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!");
  }
};