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