From 8c8a049f7761f527a69567e2f0708ddbc11f5097 Mon Sep 17 00:00:00 2001 From: hailin Date: Fri, 2 Jan 2026 19:05:54 -0800 Subject: [PATCH] fix(admin-web): handle undefined data in dashboard hooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add null-safe access and fallback to empty arrays to prevent "Cannot read properties of undefined" errors when API returns unexpected data structure. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- frontend/admin-web/src/hooks/useDashboard.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/admin-web/src/hooks/useDashboard.ts b/frontend/admin-web/src/hooks/useDashboard.ts index 4408cc71..1abbeefa 100644 --- a/frontend/admin-web/src/hooks/useDashboard.ts +++ b/frontend/admin-web/src/hooks/useDashboard.ts @@ -40,7 +40,8 @@ export function useDashboardStats() { queryKey: dashboardKeys.stats(), queryFn: async () => { const response = await dashboardService.getStats(); - return response.data.stats; + // 确保返回数组,即使 API 返回 undefined 或非数组 + return response?.data?.stats ?? []; }, staleTime: 30 * 1000, gcTime: 5 * 60 * 1000, @@ -71,7 +72,8 @@ export function useDashboardRegion() { queryKey: dashboardKeys.region(), queryFn: async () => { const response = await dashboardService.getRegionDistribution(); - return response.data.regions; + // 确保返回数组 + return response?.data?.regions ?? []; }, staleTime: 5 * 60 * 1000, // 5分钟后标记为过期 gcTime: 10 * 60 * 1000, @@ -87,7 +89,8 @@ export function useDashboardActivities(limit = 5) { queryKey: dashboardKeys.activities(limit), queryFn: async () => { const response = await dashboardService.getRecentActivities(limit); - return response.data.activities; + // 确保返回数组 + return response?.data?.activities ?? []; }, staleTime: 30 * 1000, // 30秒后标记为过期 gcTime: 5 * 60 * 1000,