medusajs/medusa

[Bug]: Deleting the last image from product media gallery crashes with "media[curr] is undefined" (not fixed in later versions)

已關閉

#15,443 建立於 2026年5月20日

 (3 則留言) (0 個反應) (0 位負責人)TypeScript (2,090 個分叉)batch import
good first issuetype: bugversion: 2.0

倉庫指標

星標
 (22,539 顆星)
PR 合併指標
 (平均合併 10天 22小時) (30 天內合併 185 個 PR)

描述

Package.json file

{
  "@medusajs/admin-sdk": "2.13.4",
  "@medusajs/cli": "2.13.4",
  "@medusajs/framework": "2.13.4",
  "@medusajs/index": "2.13.4",
  "@medusajs/medusa": "2.13.4",
  "@medusajs/test-utils": "2.13.4",
  "@medusajs/ui-preset": "^2.13.1"
}

Node.js version

v24.15.0

Database and its version

PostgreSQL 16.2

Operating system name and version

macOS 26.3.1

Browser name

Chrome

What happended?

When viewing a product's media gallery (Products → [product] → Media) and deleting the last (only) image, the page crashes with:

can't access property 'id', media[curr] is undefined

Steps to reproduce

  1. Create a product with exactly one image
  2. Navigate to the product → click "Media" to open the gallery view
  3. Click the trash icon to delete the image
  4. Confirm deletion

Expected behavior

The gallery should show the empty state ("No media" with an "Edit" button).

Actual behavior

The page crashes with a React render error. The error originates in ProductMediaGalleryCanvas component.

Root cause

In product-media-gallery.tsx, handleDeleteCurrent (line 95-96) decrements curr when deleting the last item:

if (curr === media.length - 1) {
  setCurr((prev) => prev - 1)
}

When there’s only one image, curr goes from 0 to -1. On re-render with an empty media array, media[-1] is undefined. The Canvas component (line 200/208) and Preview component (line 268) then crash accessing .isThumbnail, .url, and .id on undefined.

Fix Clamp curr to 0 minimum and add null guards before all media[curr] accesses:

setCurr((prev) => Math.max(0, prev - 1)) I wrote a patch for myself which resolves the issue: https://gist.github.com/appinteractive/22e812f2d430c46b35b8cff80677613c

Expected behavior

The gallery should show the empty state ("No media" message with an "Edit" button to add new images).

Actual behavior

The page crashes with a React render error:

can't access property 'id', media[curr] is undefined

Stack trace points to ProductMediaGalleryCanvas component in product-media-gallery.tsx.

This was previously reported in #12250 and #9135 but was auto-closed as stale without a fix.

Link to reproduction repo

https://github.com/medusajs/medusa

貢獻者指南