浏览代码

fix My Places page crash when user has no places

Backend: ensure PaginatedPlaces.Data is never nil (return empty slice)
Frontend: defensive fallback res.data || []
neyrogovnarik 1 月之前
父节点
当前提交
e52e9878cb
共有 2 个文件被更改,包括 5 次插入1 次删除
  1. 4 0
      backend/internal/handlers/places.go
  2. 1 1
      frontend/src/app/places/my/page.tsx

+ 4 - 0
backend/internal/handlers/places.go

@@ -157,6 +157,10 @@ func (h *PlaceHandler) ListMy(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	if places.Data == nil {
+		places.Data = []*models.Place{}
+	}
+
 	writeJSON(w, http.StatusOK, places)
 }
 

+ 1 - 1
frontend/src/app/places/my/page.tsx

@@ -265,7 +265,7 @@ export default function MyPlacesPage() {
   const fetchPlaces = () => {
     setLoading(true)
     api.get<{ data: Place[] }>('/places/my')
-      .then((res) => setPlaces(res.data))
+      .then((res) => setPlaces(res.data || []))
       .catch(() => {})
       .finally(() => setLoading(false))
   }