How can I access Supabase in server/routes folder of nuxt 3?

49 Views Asked by At

I'm passing supabase instance from one of my composable to the file in server/routes/allCalculators.post.ts file. By doing console.log I got to now, I'm able to access supabase instanc (db) inside composable. When I'm logging db inside server/routes/*.ts files, the object is logged, but just below it, if I accecss the a method on the object, I'm getting error as:

TypeError: db.from is not a function

Here is the code -

composable file code:

const getAllCalculators = async () => {
    const db = useSupabaseClient();
    return await $fetch('/calculators/allCalculators', {method: 'post', body: {db}});
}

export { getAllCalculators };

server/routes/allCalculators.post.ts code:

import {SupabaseClient} from '@supabase/supabase-js';

export default defineEventHandler( async(event) => {
    try {
        const { db } = await readBody(event);
        
        console.log('Logging object of Supabase', db);
        const { data, error } = await (db as SupabaseClient).from(TABLE_NAME).select('*').order('id'); /// Throws - TypeError: db.from is not a function

        return data;
    } catch (e) {
        console.error('Something went wrong while fetching', e);
    }
});

How do I solve this?

0

There are 0 best solutions below