2
votes

In my ionic app I am calling a Firebase Authentication trigger in Cloud Functions, which creates a Stripe customer. When the Stripe customer is created it stores customer id to Firebase Realtime Database. Problem comes here, the Stripe API takes time to create customer. Until then, my execute to next statement and looks for customer_id of stripe (which is still not generated) and code breaks. How do I make my code to wait until stripe user is created and customer_id is stored to database before moving to next statement.

NOTE: if I place debug point in console it waits for a moment then it goes fine

here code

createacnt() {    
    //Create Firebase account
    this.customer.Email = this.customer.Email.trim();
    this.afAuth.auth.createUserWithEmailAndPassword(this.customer.Email, this.user.password)
      .then(userDetails => {
        //Update Firebase account
        this.angularDb.object("/Customers/" + userDetails.uid).update({
            uid: userDetails.uid,
            accountid: this.customer.AccountId,            
            email: this.customer.Email            
        }).then(success => {      

          // here goes the other code in whcih 
          // I am trying to get customer id of stripe user

          this.angularDb.object('/Customers/'+userDetails.uid)
            .valueChanges().take(1).subscribe((afUser:any) =>  {

               this.user.uid = afUser.uid;
               //here code breaks and syas customer_id is undefined
               this.customer_id = afUser.payment_sources.customer_id;
          });                   
    });    
}

trigger at firebase

 exports.createStripeCustomer = functions.auth.user().onCreate(user => {
    return stripe.customers.create({
        email: user.email
    }).then(customer => {
        console.log("Stripe customer created" + customer.id);
        return admin.database().ref(`/Customers/${user.uid}/payment_sources/customer_id`).set(customer.id);
    });
});
1
This is basically the same as your other question, isn't it? Just ask one question at a time. stackoverflow.com/questions/56301600/…Doug Stevenson
@DougStevenson this is just for the wait of all executions, other is for returning data to local clientMangrio
They're basically the same issue, as far as I can tell.Doug Stevenson

1 Answers

1
votes

You can have the client listen to the location that is expected to be written by the Cloud Function. That's how you'll know when the work is complete. Don't do a one-time query. Listen to the results and proceed only when there is data found at the location.