Thibaut Chazalon
Thibaut Chazalon

Reputation: 47

Get out a value from promise Typescript

I need to get the userKey (auto-generated) for my future request:

addItemToCollection(userMail,item){

    let userKey;

    firebase.database().ref('user-list').orderByChild('email').equalTo(userMail)
    .once('value')
    .then(snapshot => {
        snapshot.forEach(function(child) {

        //userkey is the value that i want
             userKey= child.key

        });
    }); 

    //Outside the promise i need userkey Value.
    this.collectionRef=this.db.list('user-list/'+userKey+'/collection/bouteille');

    return this.collectionRef.push(item); 

}

this.db can't be find in my promise that why im there today (im new to ionic 3 and firebase)

Upvotes: 1

Views: 1723

Answers (1)

wjsc
wjsc

Reputation: 122

Make your function async, and put await before firebase call.

Upvotes: 2

Related Questions