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

标签 node.js express mongoose cloudinary

上传和设置默认 Controller 功能运行良好。然而,我们也正在尝试实现从 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' })
          );
      }
    };

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

最佳答案

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

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

cloudinary.v2.api.delete_resources(['image1', 'image2'], 函数(错误,结果){console.log(结果);});

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

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

    关于node.js - 如何通过 Node.js Express 从 Cloudinary 中删除图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53942824/

    相关文章:

    angularjs - 在 MEAN 堆栈中实现权限级别的最佳方法是什么?

    javascript - MongoDB/Meteor/Export MONGO_URL 到部署的应用程序

    javascript - 曹兰异步库系列方法的真实示例

    node.js - Node.js Express 是否包含中间件列表?

    mongodb - 如何检查文档中是否存在某个元素并根据它返回 true 或 false?

    node.js - Mongo解析错误: Unescaped at-sign in authority section at parseConnectionString

    node.js - Mocha 单元测试 Mongoose 模型

    node.js - Sqlite 如何对值进行转义以防止 SQL 注入(inject)

    javascript - Node.js/ express 邮寄不工作

    javascript - Node.js 和 Express : Pushing to array results in "[object Object]"