等到触发器在离子角的火基上触发 [英] wait until trigger is fired on firebase in ionic-angular

查看:61
本文介绍了等到触发器在离子角的火基上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的离子应用程序中,我正在Cloud Functions中调用Firebase身份验证触发器,从而创建了Stripe客户. 创建Stripe客户后,它将客户ID存储到Firebase实时数据库. 问题来了,Stripe API需要时间来创建客户.在此之前,我执行下一条语句,并寻找stripe的customer_id(仍未生成)和代码中断.我如何使代码等待创建条带用户并将customer_id存储到数据库后再移至下一条语句.

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

此处代码

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

推荐答案

您可以让客户端侦听预计由Cloud Function写入的位置.这样您才能知道工作何时完成.不要一次性查询.聆听结果,仅在该位置找到数据时继续操作.

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.

这篇关于等到触发器在离子角的火基上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆