【问题标题】:How to remove images from Cloudinary by Node.js Express?如何通过 Node.js Express 从 Cloudinary 中删除图像?
【发布时间】:2019-05-25 08:26:50
【问题描述】:

上传和设置默认控制器功能运行良好。但是,我们也在尝试从 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。我们如何在此处包含将删除图像的额外功能?

【问题讨论】:

    标签: node.js express mongoose cloudinary


    【解决方案1】:

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

    1. 通过使用管理 API。例如在节点中:

    cloudinary.v2.api.delete_resources(['image1', 'image2'], function(error, result){console.log(result);});

    1. 使用我们的上传 API:

    cloudinary.v2.uploader.destroy('sample', function(error,result) { console.log(result, error) });

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

    【讨论】:

    • 感谢您的回复。我已经编辑了问题中的代码。使用 DeleteImage 函数应该看起来像这样吗?
    • 没有。当我尝试删除图像时,没有任何反应,也没有任何错误。
    • 你能打印结果吗:console.log(res)
    猜你喜欢
    • 2020-11-05
    • 2015-09-30
    • 2020-12-18
    • 2020-05-05
    • 2020-03-02
    • 2018-01-23
    • 2016-04-28
    • 2020-08-03
    • 2019-01-30
    相关资源
    最近更新 更多