Cannot read property 'id' of null

I’m getting an error when I want to export a default realm configuration useRealm so that I will be able to use it other files

import Realm from "realm";
import { getRealmApp } from "../functions/realmConfig";
import { ItemSchema } from "./itemSchema";

export const app = getRealmApp();
export const useRealmApp = getRealmApp();

export const user = useRealmApp.currentUser;
export const partitionValue = useRealmApp.currentUser.id;

export const config = {
  schema: [ItemSchema], //other schema will be added in the future
  sync: {
    user: user,
    partitionValue: partitionValue, //app.currentUser.id,
  },
};

export const useRealm = new Realm(config);

expect results is, if the user is not logged in it should show a login screen. But it throws this error:

index.ts?77fd:9 Uncaught TypeError: Cannot read property 'id' of null
    at eval (index.ts?77fd:9)
    at Object../src/realm/index.ts (renderer.js:5394)
    at __webpack_require__ (renderer.js:791)
    at fn (renderer.js:102)
    at eval (testIndex.tsx?956d:10)
    at Object../testIndex.tsx (renderer.js:5438)
    at __webpack_require__ (renderer.js:791)
    at fn (renderer.js:102)
    at eval (App.tsx?d35d:4)

Hi Tony,

It has been a while since you posted this, are you still experiencing this error?

if the user is not logged in it should show a login screen

Could you please share the part of your code that handles this logic?

At which point does this error occur? Does it happen before or after the logic you described?

Regards
Manny

1 Like