kevin parcour
kevin parcour

Reputation: 11

Can’t use variable for angularfire2 list name in firebase(ionic)

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}`);
 
}

Upvotes: 1

Views: 39

Answers (1)

Makah
Makah

Reputation: 4513

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
    })
}

Upvotes: 1

Related Questions