Cannot read property 'uid'


#1

Hi,

I am getting this error:

TypeError: Cannot read property 'uid' of null

The code which is throwing this is:

constructor() {
    this.userProfileRef = firebase.database().ref(`userProfile/${firebase.auth().currentUser.uid}`);
  }

Not sure why… need help/suggestion


#2

Seems uid doesn’t exist at that moment. Are you using AngularFire or how or you connect with Firebase in your app?


#3

AngularFire… also it seems i need to put ionViewDidLoad() in class… Not sure, how i can do that?


#4
export class ItemProvider {

  ionViewDidLoad(){
    
  }
  public userProfileRef:firebase.database.Reference;
  constructor() {
      this.userProfileRef = firebase.database().ref(`userProfile/${firebase.auth().currentUser.uid}`);
  }

Struggling to put the code for ionViewDidLoad()


#5

uh put ionViewDidLoad() after your constructor, just as any other function inside your class. If you’re using AngularFire, why don’t follow the docs over here?

https://github.com/angular/angularfire2/blob/master/docs/Auth-with-Ionic3-Angular4.md

they are pretty well explained and work perfectly as far as I can tell :slight_smile:


#6

Sure. Am going through it right away… Thank you


#7

This is why you shouldn’t paste urls. It does not exists anymore and no information in the post on how to solve it.


#8

Next time I’ll copy the entire internet over here. There’s nothing wrong with pasting an URL to documentation my friend.

I cannot guarantee it will exist over a few seconds. And as far as this post goes, It’s a thread from 1.5 years ago. If you think this is still applicable be my guest, don’t argue about dead links. Just ask the question you want to ask if you will.


#9

Maybe if the link doesnt work, try http://www.google.com and some smart keywords?

Not sure if that link will stay though, either


#10

Hi Tommertom,
Thanks, I already solved it. I will soon post the solution for anyone who is having the same problem.


#11

Hi luukschoen,
I am used to Stackoverflow which you can’t only paste a link

Provide context for links
Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.

I think it is a good practice to do this, instead of only linking the external resource

Also, the thread is 6 months old, not 1.5 years.


#12

Finally I solved it with the session

AuthProvider

get Session() {
        return this.afAuth.authState;
}

FirebaseDbProvider

    userId: any;
    constructor(
        public afDB: AngularFireDatabase,
        public auth: AuthProvider) {
        this.auth.Session.subscribe(session => {
            if (session) {
                this.userId = session.uid;
            }
        });
    }

#13

Yes you’re absolutely right that it’s good practice. Nonetheless, this was posted june 2016. It doesn’t really matter, I agree with you that it wasn’t the best answer possible.

If you were around here a little longer you would have known that I’m quite an active contributor on this forum and I believe I’ve helped quite a lot of people around here with my answers. Little bit annoying to get such a negative answer on an old post, as where I probably could and would have helped you if you just asked.

Glad you fixed your issue and posted the solution.


#14

Yes, this is always nice to see. It drives me crazy when people just mention that they found the solution, even when I already know what it is


#15

**import { Injectable } from ‘@angular/core’;
import { AngularFire } from ‘angularfire2’;
import { User } from ‘…/shared/interfaces’;

@Injectable()
export class AuthProvicer {

constructor(public af: AngularFire) {

}

updateUser(user: User) {
let currentUserRef = this.af.database.object(/users/${user.uid});
currentUserRef.set({
id: user.id,
uid: user.uid,
username: user.username,
email: user.email,
photo: user.photo
});
}

}
**

which place import you code.??