将数据从Firebase函数返回到应用

将数据从Firebase函数返回到应用

本文介绍了将数据从Firebase函数返回到应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的离子应用程序中,我正在连接到条纹支付网关.

In my ionic app I am connecting to a stripe payment gateway.

我在firebase中有一个功能,该功能在更新客户时被触发.

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

这是我在更新客户的应用端中的代码.更新firebase中调用的触发器时,如何在下面的代码中获取触发器(firebase函数)返回的数据?

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

如果firebase函数返回错误,则需要显示触发器返回的错误消息.有什么办法吗?

If the firebase function returns an error I need to show that error message returned by the trigger. Any way to do this?

推荐答案

触发器对数据库中的事件做出反应,并且对您的应用程序一无所知.因此,触发器和应用程序之间没有链接.

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.

您可以将带有事务ID和事务状态的事务集合添加到您的用户.然后从您的应用程序中监听它,并从触发器中更新它,以在完成时得到通知.

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.

或者您可以使用可调用的云函数代替触发器. https://firebase.google.com/docs/functions/callable

Or your could use a callable cloud functions instead of a trigger.https://firebase.google.com/docs/functions/callable

这篇关于将数据从Firebase函数返回到应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:17