mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 21:24:35 +00:00
Fix Supabase connection issues with lazy initialization and enhanced logging
This commit is contained in:
@@ -6,15 +6,28 @@ import { supabase } from '@/lib/supabase/client'
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
console.log('=== SERVICES API START ===')
|
||||
console.log('Services API called with URL:', request.url)
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const locationId = searchParams.get('location_id')
|
||||
console.log('Location ID filter:', locationId)
|
||||
|
||||
// Check Supabase connection
|
||||
console.log('Supabase URL:', process.env.NEXT_PUBLIC_SUPABASE_URL)
|
||||
console.log('Supabase key exists:', !!process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY)
|
||||
// Test basic fetch to Supabase URL
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
console.log('Testing basic connectivity to Supabase...')
|
||||
try {
|
||||
const testResponse = await fetch(`${supabaseUrl}/rest/v1/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'apikey': process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
console.log('Basic Supabase connectivity test:', testResponse.status, testResponse.statusText)
|
||||
} catch (fetchError) {
|
||||
console.error('Basic fetch test failed:', fetchError)
|
||||
}
|
||||
|
||||
let query = supabase
|
||||
.from('services')
|
||||
@@ -26,10 +39,10 @@ export async function GET(request: NextRequest) {
|
||||
query = query.eq('location_id', locationId)
|
||||
}
|
||||
|
||||
console.log('Executing query...')
|
||||
console.log('Executing Supabase query...')
|
||||
const { data: servicesData, error: queryError } = await query
|
||||
|
||||
console.log('Query result - data:', !!servicesData, 'error:', !!queryError)
|
||||
console.log('Query result - data exists:', !!servicesData, 'error exists:', !!queryError)
|
||||
|
||||
if (queryError) {
|
||||
console.error('Services GET error details:', {
|
||||
@@ -42,25 +55,31 @@ export async function GET(request: NextRequest) {
|
||||
{
|
||||
error: queryError.message,
|
||||
code: queryError.code,
|
||||
details: queryError.details
|
||||
details: queryError.details,
|
||||
timestamp: new Date().toISOString()
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Services found:', servicesData?.length || 0)
|
||||
console.log('=== SERVICES API END ===')
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
services: servicesData || [],
|
||||
count: servicesData?.length || 0
|
||||
count: servicesData?.length || 0,
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('=== SERVICES API ERROR ===')
|
||||
console.error('Services GET unexpected error:', error)
|
||||
console.error('Error stack:', error instanceof Error ? error.stack : 'Unknown error')
|
||||
console.error('Error type:', error instanceof Error ? error.constructor.name : typeof error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Internal server error',
|
||||
details: error instanceof Error ? error.message : 'Unknown error'
|
||||
details: error instanceof Error ? error.message : 'Unknown error',
|
||||
timestamp: new Date().toISOString()
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user