🔧 Fix Thumbnails API SQL Result Structure
🐛 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! 🎯
This commit is contained in:
parent
2951efdaa0
commit
39dbaca07d
1 changed files with 33 additions and 38 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue