Home > database >  Can't fetch data with supabase in nodejs
Can't fetch data with supabase in nodejs

Time:01-01

I'm trying to implement supabase within my nodejs api but everytime i call the endpoint returns an error: TypeError: supabase.from is not a function. The function that is firing the supabase function is:

// Supabase client
const supabase = require('@supabase/supabase-js');
console.log(supabase)

const stripeTest = async(req, res) => {
  console.log('hellooo!!!!!');
 
  try {
    const {data: events} = await supabase
    .from('events')
    .select('id')

    console.log('events', events)
    
  } catch (error) {
    console.log('error ', error);
  }

}

Supabase is configured in a file called config.js:

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

const supabaseUrl = process.env.APP_SUPABASE_URL;
const supabaseAnonKey = process.env.APP_SUPABASE_ANON_KEY;

const supabase = createClient(supabaseUrl, supabaseAnonKey)

The library that i'm using is: supabase-js

CodePudding user response:

You should export the client from config.js:

const { createClient } = require('@supabase/supabase-js');

const supabaseUrl = process.env.APP_SUPABASE_URL;
const supabaseAnonKey = process.env.APP_SUPABASE_ANON_KEY;

module.exports = createClient(supabaseUrl, supabaseAnonKey);

And use that in your function:

const supabase = require('./path/to/config.js');

const stripeTest = async(req, res) => {
  console.log('hellooo!!!!!');
 
  try {
    const {data: events} = await supabase
    .from('events')
    .select('id')

    console.log('events', events)
    
  } catch (error) {
    console.log('error ', error);
  }
}
  • Related