fix: fetch 后检查 res.ok 并校验数组类型,防止错误响应导致崩溃
- panic/page.tsx: suggestions fetch 检查 res.ok + Array.isArray - profile/page.tsx: history/favorites fetch 检查 res.ok + Array.isArray
This commit is contained in:
@@ -70,9 +70,10 @@ export default function PanicPage() {
|
|||||||
setFetchingSuggestions(true);
|
setFetchingSuggestions(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/location/suggest?keywords=${encodeURIComponent(query)}`);
|
const res = await fetch(`/api/location/suggest?keywords=${encodeURIComponent(query)}`);
|
||||||
|
if (!res.ok) { setSuggestions([]); setShowSuggestions(false); return; }
|
||||||
const data: LocationSuggestion[] = await res.json();
|
const data: LocationSuggestion[] = await res.json();
|
||||||
setSuggestions(data);
|
setSuggestions(Array.isArray(data) ? data : []);
|
||||||
setShowSuggestions(data.length > 0);
|
setShowSuggestions(Array.isArray(data) && data.length > 0);
|
||||||
} catch {
|
} catch {
|
||||||
setSuggestions([]);
|
setSuggestions([]);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -93,15 +93,15 @@ export default function ProfilePage() {
|
|||||||
|
|
||||||
setHistoryLoading(true);
|
setHistoryLoading(true);
|
||||||
fetch(`/api/user/history?userId=${userId}`)
|
fetch(`/api/user/history?userId=${userId}`)
|
||||||
.then((r) => r.json())
|
.then((r) => { if (!r.ok) throw new Error(); return r.json(); })
|
||||||
.then(setHistory)
|
.then((data) => setHistory(Array.isArray(data) ? data : []))
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
.finally(() => setHistoryLoading(false));
|
.finally(() => setHistoryLoading(false));
|
||||||
|
|
||||||
setFavLoading(true);
|
setFavLoading(true);
|
||||||
fetch(`/api/user/favorite?userId=${userId}`)
|
fetch(`/api/user/favorite?userId=${userId}`)
|
||||||
.then((r) => r.json())
|
.then((r) => { if (!r.ok) throw new Error(); return r.json(); })
|
||||||
.then(setFavorites)
|
.then((data) => setFavorites(Array.isArray(data) ? data : []))
|
||||||
.catch(() => {})
|
.catch(() => {})
|
||||||
.finally(() => setFavLoading(false));
|
.finally(() => setFavLoading(false));
|
||||||
}, [userId]);
|
}, [userId]);
|
||||||
|
|||||||
Reference in New Issue
Block a user