From 39dbaca07dad54b90d2a17833f5bca587980f82f Mon Sep 17 00:00:00 2001 From: Randall Stillwell Date: Sun, 27 Jul 2025 14:15:29 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20Thumbnails=20API=20SQL=20R?= =?UTF-8?q?esult=20Structure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 Root Cause Found: - SQL queries return { rows: [...] } structure, not direct arrays - Code was accessing collectionResult.length instead of collectionResult.rows.length - This caused undefined results leading to collection.id errors ✅ Fixes Applied: - Updated to use collectionResult.rows.length for length checks - Updated to use collectionResult.rows[0] for collection data - Added proper SQL error handling with try/catch - Enhanced validation for SQL result structure 🔧 Technical Improvements: - Proper error handling for SQL query failures - Correct access to SQL result structure - Better validation before accessing collection properties - Cleaner debug output (removed excessive logging) This should resolve the 'Cannot read properties of undefined (reading 'id')' error! 🎯 --- .../collections/[identifier]/thumbnails.js | 71 +++++++++---------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/pages/api/collections/[identifier]/thumbnails.js b/pages/api/collections/[identifier]/thumbnails.js index 163ab8e..612d244 100644 --- a/pages/api/collections/[identifier]/thumbnails.js +++ b/pages/api/collections/[identifier]/thumbnails.js @@ -34,51 +34,46 @@ export default async function handler(req, res) { // Determine if identifier is a slug or numeric ID const isSlug = isValidSlug(identifier) || isNaN(parseInt(identifier)); - console.log('🔍 Identifier analysis:', { identifier, isSlug, isValidSlug: isValidSlug(identifier), isNaN: isNaN(parseInt(identifier)) }); - // Verify user has access to this collection let collectionResult; - if (isSlug) { - console.log('🔍 Querying by slug:', identifier); - collectionResult = await sql` - SELECT c.*, cp.role as user_role - FROM collections c - LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' - WHERE c.slug = ${identifier} - AND ( - c.user_id = ${user.userId} OR - cp.id IS NOT NULL OR - c.is_public = true - ) - `; - } else { - const numericId = parseInt(identifier); - console.log('🔍 Querying by ID:', numericId); - collectionResult = await sql` - SELECT c.*, cp.role as user_role - FROM collections c - LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' - WHERE c.id = ${numericId} - AND ( - c.user_id = ${user.userId} OR - cp.id IS NOT NULL OR - c.is_public = true - ) - `; + try { + if (isSlug) { + collectionResult = await sql` + SELECT c.*, cp.role as user_role + FROM collections c + LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' + WHERE c.slug = ${identifier} + AND ( + c.user_id = ${user.userId} OR + cp.id IS NOT NULL OR + c.is_public = true + ) + `; + } else { + const numericId = parseInt(identifier); + collectionResult = await sql` + SELECT c.*, cp.role as user_role + FROM collections c + LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = ${user.userId} AND cp.status = 'active' + WHERE c.id = ${numericId} + AND ( + c.user_id = ${user.userId} OR + cp.id IS NOT NULL OR + c.is_public = true + ) + `; + } + + } catch (sqlError) { + console.error('🔍 SQL Error:', sqlError); + return res.status(500).json({ error: 'Database query failed' }); } - if (collectionResult.length === 0) { + if (!collectionResult || !collectionResult.rows || collectionResult.rows.length === 0) { return res.status(404).json({ error: 'Collection not found or access denied' }); } - console.log('🔍 Collection result:', { - identifier, - isSlug, - resultLength: collectionResult.length, - collection: collectionResult[0] - }); - - const collection = collectionResult[0]; + const collection = collectionResult.rows[0]; // Add debugging and validation if (!collection || !collection.id) {