1

I can’t use a variable to hold the name of my list on firebase.

After I push some data the variable becomes undefined in firebase.

$this.UserID is meant to hold the UID but it can't be read.

My code:

private referenceListRef;
UserID: string;

constructor(private http: Http, 
            private db: AngularFireDatabase,
            private ofAuth: AngularFireAuth,
            public storage: Storage
) {
    this.ofAuth.authState.take(1).subscribe(data => {
        this.UserID = data.uid;
        console.log(`user id : ${this.UserID}`);
    })
    this.referenceListRef =  this.db.list(`${this.UserID}`);
 
}

1
  • im sorry, i was kinda confused as what to type, or to ask so perhaps the question was a bit confusing. and again english isn't my main language. so thank you so much for editing that post. now that looks like a proper question! Commented Dec 28, 2017 at 14:45

1 Answer 1

1

You need to add referenceListRef inside subscribe(), because you don't have this.UserID before this.ofAuth.authState Observable's completes.

private referenceListRef;
UserID: string;

constructor(private http: Http, 
            private db: AngularFireDatabase,
            private ofAuth: AngularFireAuth,
            public storage: Storage
) {
    this.ofAuth.authState.take(1).subscribe(data => {
        this.UserID = data.uid;
        console.log(`user id : ${this.UserID}`);
        this.referenceListRef =  this.db.list(`${this.UserID}`); // <-- This code
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! i tried it but unfortunately now i get this error "cannot read property snapshotchanges of undefined"
wait, turns out if i click close after the error, which will send me back to the login page and i re-login, it works. but somehow if i restart the app the errorwill come out again. so i have to close the error message and re login. but hey it worked! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.