angelcervera
angelcervera

Reputation: 4189

Nanostore triggering an exception when used from inside onAuthStateChanged

When I'm using an atom store (from the nanostores library) from inside the onAuthStateChanged, I'm getting an error: Cannot read properties of undefined (reading '_freezeSettings'). From outside, it is working fine.

UserStore.ts file:


    import {app} from "@js/firebase_init"
    import {getAuth, onAuthStateChanged} from "firebase/auth";
    import {atom} from "nanostores";

    const auth = getAuth(app);

    const userStore = atom <String|undefined>(undefined);

    onAuthStateChanged(auth, (user) => {
      if (user) {
        userStore.set("No matter what to set, I get an error"); // <--- Error here
      } else {
        userStore.set(undefined);
      }
    });

    export {userStore};

Error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_freezeSettings')
    at qh (index.esm2017.js:18716:17)
    at wl._parse (index.esm2017.js:19286:19)
    at wl._apply (index.esm2017.js:19282:24)
    at dl (index.esm2017.js:19259:30)
    at Array.<anonymous> (CartStore.ts:26:19)
    at Object.notify (index.js:55:28)
    at Object.set (index.js:16:13)
    at UserStore.ts:11:19
    at auth_impl.ts:565:24

This is the code that is triggering the error:

userStore.listen( (user) =>  {
    if(user) {
        const q = query( // <-- This is the code triggering the error
            "collectionName",
            where("userId", "==", user.uid)
        )
    }
});

Removing the query from the listener, the error does not appears.

Versions:

Upvotes: 1

Views: 180

Answers (0)

Related Questions