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

贡献者指南