From e93381c9f1ea8e713e07c7e7d0fec14bd6520596 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Sun, 15 Mar 2026 15:06:42 -0700 Subject: [PATCH] Fix: parse Surreal query results safely --- app/api/trades/route.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/api/trades/route.js b/app/api/trades/route.js index 3bb8436..4ba3e1c 100644 --- a/app/api/trades/route.js +++ b/app/api/trades/route.js @@ -3,23 +3,39 @@ import Surreal from 'surrealdb'; export const dynamic = 'force-dynamic'; +function normalizeRows(result) { + if (!Array.isArray(result) || !result.length) return []; + const first = result[0]; + if (Array.isArray(first)) return first; + if (first && typeof first === 'object' && Array.isArray(first.result)) return first.result; + return []; +} + export async function GET() { const url = process.env.SURREAL_URL; if (!url) { return NextResponse.json({ trades: [], error: 'No DB configured' }); } + let client = null; + try { - const client = new Surreal(); + client = new Surreal(); await client.connect(url); await client.signin({ username: process.env.SURREAL_USER, password: process.env.SURREAL_PASS }); await client.use({ namespace: 'kalbot', database: 'kalbot' }); const result = await client.query('SELECT * FROM paper_positions ORDER BY entryTime DESC LIMIT 50'); - const trades = result[0] || []; + const trades = normalizeRows(result); return NextResponse.json({ trades }); } catch (e) { return NextResponse.json({ trades: [], error: e.message }); + } finally { + try { + await client?.close?.(); + } catch { + // ignore + } } }