В моем ионном приложении я вызываю триггер Firebase Authentication в Cloud Functions, который создает клиента Stripe.
Когда клиент Stripe создан, он сохраняет идентификатор клиента в базе данных Firebase Realtime.
Проблема возникает здесь, 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);
});
});