timofeysie / khipu

Electron PWA starter
MIT License
4 stars 1 forks source link

Cannot read property 'uid' of null #32

Closed timofeysie closed 3 years ago

timofeysie commented 3 years ago

at RealtimeDbService.push../src/app/core/firebase/realtime-db.service.ts.RealtimeDbService.setupFirebase (realtime-db.service.ts:147)

timofeysie commented 3 years ago

This is using the firebase realtime database for we. The solution was to check if the user object exists first.

    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        const database = firebase.database();
        this.userId = firebase.auth().currentUser.uid;
      }
    });

Not sure if this is the final solution, but will reopen this if needed.

timofeysie commented 3 years ago

What seemed to work for this, was returning the userId from the setup function, rather than doing the two as separate calls.

const userId = this.setupFirebase();
...
setupFirebase() {
    const firebaseConfig = {
      apiKey: 'xxx',
     ...
    };
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig);
    }
    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        const database = firebase.database();
      }
      return firebase.auth().currentUser.uid;
    });
  }

It seems pretty weak, but has appeared to solve the problem when used like this.