Kavindu Gayantha
Kavindu Gayantha

Reputation: 376

undefined is not an object(evaluating 'this.localStorage.getItem') error in react-native

I tried to implement supabase with react-native expo app. Then got this error when trying to call a GET API call to get all data from 'Players' table but got an error in localstorage. How to fix this react-native supabase error

const getAllPlayers = async () => {
        try {

            let {data} = await supabase
                .from('players')
                .select('*')

            console.log("all players", JSON.parse(data));
        } catch (error) {
            console.log('error', error);
        }
    }
   

Can't we access directly supabase just with a react-native app ?

Upvotes: 0

Views: 824

Answers (1)

baumstumpf
baumstumpf

Reputation: 192

I had the same issue recently. It was gone after I added options to the createClient call of supabase in which I defined the storage for the auth object. Looking like this:

import 'react-native-url-polyfill/auto';
import AsyncStorage from '@react-native-async-storage/async-storage';

const options = {
  auth: {
    storage: AsyncStorage,
  },
}

export const supabase = createClient(url, key, options);

Btw. I also needed the polyfill for this to work in React Native.

Hope this helps.

Upvotes: 2

Related Questions