问题
In my ionic app I am connecting to a stripe payment gateway.
I have a function in firebase which is being triggered on updating the customer.
exports.updateStripeCustomer = functions.database.ref("/Customers/{userId}")
.onUpdate((snapshot, context) => {
const data = snapshot.after.val();
return stripe.customers.createSource(data.payment_sources.customer_id, {
source: data.payment_sources.source
}).then(customer => {
console.log("Update Stripe Customer");
return customer;
},
error => {
return error;
}
);
});
This is my code in the app end where I am updating the customer. While updating the trigger called in firebase, how do I get the data returned by the trigger (firebase function) in my below code?
this.angularDb.object('/Customers/' + firebase.auth().currentUser.uid).update({
cardsetup: 1,
payment_sources: {
customer_id: this.user.customer_id,
source: this.card.source.id
}
}).then((res) => {
loading.dismiss();
alert("card details has been successfully updated.");
this.navCtrl.push(LoginPage);
}, (error) => {
loading.dismiss();
console.log("=========>", error.message)
alert(JSON.stringify(error))
});
If the firebase function returns an error I need to show that error message returned by the trigger. Any way to do this?
回答1:
The trigger reacts to an event in the database and doesn't know anything about your application. Therefore there is no link between your trigger and your application.
You could add a Transaction collection to your users with a transaction id and a transaction status. Then listen to it from your application and update it from the trigger to get notified when it is complete.
Or your could use a callable cloud functions instead of a trigger. https://firebase.google.com/docs/functions/callable
来源:https://stackoverflow.com/questions/56301600/return-data-from-firebase-function-to-app