简体   繁体   English

cloudinary的销毁方法没有反应

[英]no response from destroy method of cloudinary

I am trying to delete the images from the client-side but destroy method not working, does not delete the images and at the same not giving me any error.我正在尝试从客户端删除图像,但破坏方法不起作用,不会删除图像,同时也不会给我任何错误。

I am working with React.js and this is my method:我正在使用 React.js,这是我的方法:

    deleteProductHandler = id => {

db.collection("products")
  .doc(id)
  .delete()
  .then(() => {

    // update the UI
    const products = [...this.state.products];
    let images = [];
    products.forEach((product, index) => {
      if (product.id === id) {
        products.splice(index, 1);
        images = [...product.images];
        this.setState({ show: false, products: products });
      }
    });

    // delete images from  cloudinary
    let links = images
      .map(link => {
        return link.match("products/");
      })
      .map(link => {
        const newlink = link.input.slice(link.index);
        const newlink2 = newlink.slice(0, -4);
        return { publicId: newlink2 };
      });

    let publicIds = [];
    for (let key in links) {
      publicIds.push(links[key].publicId);
    }

    console.log(publicIds);  
    // i got all publicIds here without any problem.
    // so dont wory about the code above.
    
    publicIds.forEach(publicId => {
      console.log(publicId);
      window.cloudinary.v2.uploader().destroy(publicId, (err, res) => {
      console.log(err, res);
      });
    });
  })
  .catch(err => {
    this.setState({ error: err });
  });
  };

this is the documentation of destroy method: https://cloudinary.com/documentation/image_upload_api_reference#destroy_method这是销毁方法的文档: https://cloudinary.com/documentation/image_upload_api_reference#destroy_method

what I am trying to get in here is that when the user deletes a product, automatically its images will be deleted from cloudinary.我想在这里说明的是,当用户删除产品时,它的图像会自动从云端删除。

I think the extra () after uploader might be the problem.我认为uploader之后的额外()可能是问题所在。

It should be它应该是

window.cloudinary.v2.uploader.destroy(publicId, (err, res) => {
      console.log(err, res);
      });

It's not possible to delete images from the client-side directly because there's no way to authenticate such a request using details stored in the client - if you had your account's API secret available to the client, any of your users could use that secret to make arbitrary API calls to your account.无法直接从客户端删除图像,因为无法使用存储在客户端中的详细信息来验证此类请求 - 如果您的帐户的 API 密码可供客户端使用,您的任何用户都可以使用该密码来制作任意 API 调用您的帐户。

In my app, I don't have any server-side component because I was working with firebase firestore, and what I am trying to do in the code above is to render destroy method in the client-side code, which is not possible.在我的应用程序中,我没有任何服务器端组件,因为我正在使用 firebase firestore,而我在上面的代码中尝试做的是在客户端代码中呈现销毁方法,这是不可能的。

instead, you need a server-side component probably in node.js.相反,您可能需要 node.js 中的服务器端组件。 that's a logical place to add a controller to handle deletion or other similar actions.这是添加 controller 来处理删除或其他类似操作的合乎逻辑的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM