首页 > 解决方案 > 等到在 ionic-angular 的 firebase 上触发触发器

问题描述

在我的 ionic 应用程序中,我在 Cloud Functions 中调用 Firebase 身份验证触发器,该触发器创建了一个 Stripe 客户。创建 Stripe 客户后,它将客户 ID 存储到 Firebase 实时数据库。问题来了,Stripe API 需要时间来创建客户。在那之前,我执行到下一条语句并查找条带的 customer_id(仍未生成)和代码中断。如何让我的代码等到创建条带用户并将 customer_id 存储到数据库中,然后再移动到下一条语句。

注意:如果我将调试点放在控制台中,它会等待片刻然后一切正常

这里代码

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

在火力基地触发

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

标签: angulartypescriptfirebasegoogle-cloud-functionsstripe-payments

解决方案


您可以让客户端侦听预计由 Cloud Function 写入的位置。这样您就可以知道工作何时完成。不要进行一次性查询。听取结果并仅在该位置找到数据时继续。


推荐阅读