如何通过Node.js Express从Cloudinary中删除图像?

如何通过Node.js Express从Cloudinary中删除图像?,node.js,express,mongoose,cloudinary,Node.js,Express,Mongoose,Cloudinary,上传和设置默认控制器功能工作正常。然而,我们也在尝试从Cloudinary中实现删除图像。怎样才能做到呢? 在文档中,它令人困惑。代码如下: const cloudinary = require('cloudinary'); const HttpStatus = require('http-status-codes'); const User = require('../models/userModels'); cloudinary.config({ c

上传和设置默认控制器功能工作正常。然而,我们也在尝试从Cloudinary中实现删除图像。怎样才能做到呢? 在文档中,它令人困惑。代码如下:

 const cloudinary = require('cloudinary');
    const HttpStatus = require('http-status-codes');

    const User = require('../models/userModels');

    cloudinary.config({
      cloud_name: 'name',
      api_key: 'key',
      api_secret: 'secret'
    });

    module.exports = {
      UploadImage(req, res) {
        cloudinary.uploader.upload(req.body.image, async result => {
          await User.update(
            {
              _id: req.user._id
            },
            {
              $push: {
                images: {
                  imgId: result.public_id,
                  imgVersion: result.version
                }
              }
            }
          )
            .then(() =>
              res
                .status(HttpStatus.OK)
                .json({ message: 'Image uploaded successfully' })
            )
            .catch(err =>
              res
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .json({ message: 'Error uploading image' })
            );
        });
      },

      DeleteImage(req, res) {
    cloudinary.uploader.destroy(req.params.image, async result => {
      await User.update(
        {
          _id: req.user._id
        },
        {
          $pull: {
            images: {
              imgId: result.public_id,
              imgVersion: result.version
            }
          }
        }
      )
        .then(() =>
          res
            .status(HttpStatus.OK)
            .json({ message: 'Image deleted successfully' })
        )
        .catch(err =>
          res
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .json({ message: 'Error deleting image' })
        );
    });
  },

      async SetDefaultImage(req, res) {
        const { imgId, imgVersion } = req.params;

        await User.update(
          {
            _id: req.user._id
          },
          {
            picId: imgId,
            picVersion: imgVersion
          }
        )
          .then(() =>
            res.status(HttpStatus.OK).json({ message: 'Default image set' })
          )
          .catch(err =>
            res
              .status(HttpStatus.INTERNAL_SERVER_ERROR)
              .json({ message: 'Error occured' })
          );
      }
    };

我们在Mongoose中使用Node.js Express。我们如何在这里包含删除图像的额外功能

从cloudinary中删除图像有两个选项:

  • 通过使用管理API。例如,在节点中:
  • 
    cloudinary.v2.api.delete_资源(['image1','image2'],
    函数(错误,结果){console.log(结果);});
    

  • 使用我们的上传API:
  • 
    cloudinary.v2.uploader.destroy('sample',函数(错误,结果){
    console.log(结果,错误)});
    


    请注意,使用我们的管理API是有速率限制的,您可能需要使用第二个选项。

    谢谢您的回复。我已经编辑了问题中的代码。使用DeleteImage函数时应该是这样吗?不。当我尝试删除图像时,没有任何事情发生,也没有任何错误。您可以打印结果:
    console.log(res)