Search code examples
javascriptreact-nativeexposupabase

Why is supabase not returning any data when I still have rows of data in my table on react native?


I am using expo router react native and I am implementing supabase as a database option in my app. I have a console.log of the data from the select function but everytime I run it I get [] logged and nothing else. My select function can't read the data in my tables on supabase (my tables do have rows of data in it) How do I fix this issue? Here is the code

const checkTransferForums = async () => {
    const {
      data: { user: User },
    } = await supabase.auth.getUser();

    const { data, error } = await supabase.from('user_forums').select('user_id');

    if (error) {
      console.log('🚀 ~ checkTransferForums ~ error:', error);
      return;
    }
    console.log(data);

    if (data && User) {
      for (const row of data) {
        console.log("In for loop");
        if (row['user_id'] === User?.id) {
          router.replace('/(auth)/');
          console.log('Transferring to home');
        } else {
          router.replace('/forum/userForum');
          console.log('Transferring to the forums');
        }
      }
    }
  };

I tried restarting my project a few times and I've ran the code at different files in my project, but they all return the same thing. I am expecting to see some form of data being returned in the array.


Solution

  • It appears to be a policy issue. One potential solution could be to adjust the policy to enable read access for all users on the "public"."food" table, like so:

    ALTER POLICY "Enable read access for all users"
    ON "public"."nameTable"
    TO public
    USING (
       true
    );
    

    This statement grants read access to all users by setting the condition to always evaluate to true. Make sure to adjust the policy name and table name as necessary in your environment.