Fix list: search all memories with prefix

This commit is contained in:
Manohar Gupta 2026-05-10 14:40:33 +05:30
parent 1c352dade1
commit 3e63930e21

View file

@ -34,17 +34,14 @@ export async function GET(req: NextRequest) {
try { try {
const { client, bucket, baseUrl } = getR2(); const { client, bucket, baseUrl } = getR2();
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);
const childId = searchParams.get("childId") || "default"; const prefix = searchParams.get("childId") || "default";
const prefix = `memories/${childId}`;
const command = new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix, MaxKeys: 10 }); // List all objects with memories/ prefix
const command = new ListObjectsV2Command({ Bucket: bucket, Prefix: "memories/", MaxKeys: 50 });
const res = await client.send(command); const res = await client.send(command);
// Debug: log what we found
console.log("List result:", JSON.stringify(res));
if (!res.Contents || res.Contents.length === 0) { if (!res.Contents || res.Contents.length === 0) {
return NextResponse.json({ memories: [], debug: { bucket, prefix, count: 0 } }); return NextResponse.json({ memories: [], debug: "no files found" });
} }
const objects = res.Contents.map((obj) => ({ const objects = res.Contents.map((obj) => ({
@ -57,7 +54,7 @@ export async function GET(req: NextRequest) {
return NextResponse.json({ memories: objects }); return NextResponse.json({ memories: objects });
} catch (error) { } catch (error) {
console.error("R2 list error:", error); console.error("R2 list error:", error);
return NextResponse.json({ error: String(error), stack: error?.stack }, { status: 500 }); return NextResponse.json({ error: String(error) }, { status: 500 });
} }
} }