🐛 Add Debug Logging for Thumbnails API Error

Added comprehensive debugging to identify why collection.id is undefined:
- Log identifier analysis (slug vs ID detection)
- Log which query path is taken (slug vs numeric ID)
- Log collection result structure and content
- Add validation for collection data before using collection.id
- Better error messages for debugging

This will help identify the root cause of the thumbnails API failure.
This commit is contained in:
Randall Stillwell 2025-07-27 14:11:51 -05:00
parent 00e3a3a902
commit 2951efdaa0

View file

@ -34,9 +34,12 @@ 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
@ -50,6 +53,7 @@ export default async function handler(req, res) {
`;
} else {
const numericId = parseInt(identifier);
console.log('🔍 Querying by ID:', numericId);
collectionResult = await sql`
SELECT c.*, cp.role as user_role
FROM collections c
@ -67,8 +71,21 @@ export default async function handler(req, res) {
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];
// Add debugging and validation
if (!collection || !collection.id) {
console.error('Collection data invalid:', { collection, identifier, isSlug });
return res.status(500).json({ error: 'Collection data invalid' });
}
// Get the top 5 rarest cards from the collection
const thumbnailsResult = await sql`
SELECT DISTINCT