angular - 等到在 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);
});
});
解决方案
您可以让客户端侦听预计由 Cloud Function 写入的位置。这样您就可以知道工作何时完成。不要进行一次性查询。听取结果并仅在该位置找到数据时继续。
推荐阅读
- python - 如何在带有日期索引和缺失日期的 Pandas 数据框中选择下一个日期
- sql - 基于同一字段中的 2 个值的条件分组
- python - 使用python进行重复数据删除
- python - 你如何计算字典中列表的元素?
- django - 有什么聪明的方法可以在 Django 中使用 Web 套接字保存文件?
- javascript - How to write this statement without if/else?
- vb.net - Visual Basic - Password Validation
- java - 无法使用 redisson 连接到 redis 实例
- spring-boot - Spring Boot 外键返回 null
- c# - 如何在c#中打开两个表单之间传递参数