cloudinary-community / next-cloudinary

⚡️ High-performance image delivery and uploading at scale in Next.js powered by Cloudinary.
https://next.cloudinary.dev
MIT License
252 stars 75 forks source link

[BUG] Next Cloudinary cannot read properties of undefined(reading 'open') #489

Closed GracieRen closed 2 months ago

GracieRen commented 2 months ago

Uploader occasionally doesn't always launch, it is a blocker for users to upload image/video.

No upload modal pops up. Checked the console and error is shown as below

image
colbyfayock commented 2 months ago

hey @GracieRen i suspect the issue may be that the upload widget scripts haven't loaded yet im wondering if you look at the network tab for the script files if when you see this error, have they not fully completed?

if that is the case, not much i can do beyond trying to add additional logic to check if the script is loaded yet as this simply wraps the Cloudinary Upload Widget script, but the open method, if using the CldUploadWidget, is called from the UI that you control, so you can check if the widget instance exists before triggering that method

<CldUploadWidget ... >
  {({ open } = {}) => { // make sure to set the default value of an empty object in case the argument isnt defined
    function handleOnClick() {
      if ( typeof open === 'function' ) {
        open();
      }
    }
    return (
      <button onClick={handleOnClick}>
        Upload an Image
      </button>
    );
  }}
</CldUploadWidget>

disabling/showing a spinner on the button until it's loaded, as it's also passed a isLoading argument

<CldUploadWidget ... >
  {({ isLoading, open } = {}) => { // make sure to set the default value of an empty object in case the argument isnt defined
    function handleOnClick() {
      if ( typeof open === 'function' ) {
        open();
      }
    }
    return (
      <button onClick={handleOnClick} disabled={isLoading}> // or simply return inside of `handleOnClick` if `isLoading`
        Upload an Image
      </button>
    );
  }}
</CldUploadWidget>
colbyfayock commented 2 months ago

going to close this since i haven't heard back but please let me know if you're still having issues after trying the above