From 2e969f68b4e10a7e01219e8d5e352a8ecbe87917 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 28 Feb 2026 05:49:42 +0000 Subject: [PATCH] fix: parse JSONB string from asyncpg + frontend parseVal fallback --- backend/main.py | 9 ++++++++- frontend/app/signals/page.tsx | 17 ++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/backend/main.py b/backend/main.py index 36f6632..7dbc0ad 100644 --- a/backend/main.py +++ b/backend/main.py @@ -449,7 +449,14 @@ async def get_market_indicators(user: dict = Depends(get_current_user)): ind_type, ) if row: - indicators[ind_type] = {"value": row["value"], "ts": row["timestamp_ms"]} + val = row["value"] + if isinstance(val, str): + import json as _json + try: + val = _json.loads(val) + except Exception: + pass + indicators[ind_type] = {"value": val, "ts": row["timestamp_ms"]} result[sym.replace("USDT", "")] = indicators return result diff --git a/frontend/app/signals/page.tsx b/frontend/app/signals/page.tsx index a23e32c..52a16a3 100644 --- a/frontend/app/signals/page.tsx +++ b/frontend/app/signals/page.tsx @@ -115,11 +115,18 @@ function MarketIndicatorsCards({ symbol }: { symbol: Symbol }) { if (!data) return
等待市场指标数据...
; - // value是JSONB对象,需要取具体字段 - const lsVal = data.long_short_ratio?.value as Record | undefined; - const topVal = data.top_trader_position?.value as Record | undefined; - const oiVal = data.open_interest_hist?.value as Record | undefined; - const premVal = data.coinbase_premium?.value as Record | undefined; + // value可能是JSON字符串或对象,统一解析 + const parseVal = (v: unknown): Record => { + if (!v) return {}; + if (typeof v === "string") { try { return JSON.parse(v); } catch { return {}; } } + if (typeof v === "object") return v as Record; + return {}; + }; + + const lsVal = parseVal(data.long_short_ratio?.value); + const topVal = parseVal(data.top_trader_position?.value); + const oiVal = parseVal(data.open_interest_hist?.value); + const premVal = parseVal(data.coinbase_premium?.value); const longPct = Number(lsVal?.longAccount ?? 0.5) * 100; const shortPct = Number(lsVal?.shortAccount ?? 0.5) * 100;