doesSessionExist is undefined error
# support-questions
a
Hi! i'm actually tryin to connect to Hannes Colt's backend by updating the front end sdk to latest, and grabbing the "Supertokens.getAccessToken()" instaed of the old "doesSessionExist()" or "attemptRefreshSession()". i'm getting a weird error that"doesSessionExist is undefined" when i'm running the "getAccessToken()", something is wrong in the setup but i can't really find it :/
r
hey @Axel Jönsson
a
hi!
r
can you share the error stack trace?
also, share your code along with the import statement
a
it's very small i'm affraid: " ERROR [TypeError: Cannot read property 'doesSessionExist' of undefined]"
r
i see.
a
this is the hook:
Copy code
import {useCallback} from 'react';
import SuperTokens from 'supertokens-react-native';

interface InitUserSession {
  session: boolean;
  userId: string;
}

export default function useGetSuperTokensDataHook() {
  const _onError = useCallback((err: unknown) => {
    console.error(err);
    throw new Error('Error in initUserSession');
  }, []);

  const getSuperTokensData = useCallback(async (): Promise<
    InitUserSession | undefined
> => {
    try {
      const data = await SuperTokens?.getAccessToken();
      console.log('accesstoken--->', data);
      //Get the data we require from supertokens
      const session = await SuperTokens.attemptRefreshingSession();
      const userId = await SuperTokens.getUserId();

      if (!session) {
        throw new Error('no session');
      }
      if (!userId) {
        throw new Error('no connected user');
      }

      //return successfull promise
      return {session, userId};
    } catch (err) {
      _onError(err);
    }
  }, [_onError]);

  return {
    getSuperTokensData,
  };
}
r
@nkshah2 can help here
a
and the init function if you need it:
Copy code
import {BACKEND_URL, SUPERTOKENS_COOKIE_DOMAIN} from '@lib/api.config';
import SuperTokens from 'supertokens-react-native';

export function initSuperTokens() {
  SuperTokens.init({
    apiDomain: BACKEND_URL,
    sessionTokenBackendDomain: SUPERTOKENS_COOKIE_DOMAIN,
  });
}
n
Hey @Axel Jönsson Can you check if the supertokens init call happens before you try to use getAccessToken? (Just add logs before the two to make sure as well)
a
yeah, it definatly happens before
n
Will be easier to get this debugged on call, you can join here: https://meet.google.com/bem-giwe-cbp
Just to update, the issue was that the init call was inside a useEffect but one of the children needed access to getAccessToken. Because the init call would happen after the component rendered (since useEffect triggers after mounting) it was complaining about something internal being undefined which is expected