【问题标题】:Return data from firebase functions back to android [duplicate]将数据从firebase函数返回到android [重复]
【发布时间】:2021-06-21 23:08:59
【问题描述】:

我想做什么:只需从 firebase 云函数返回数据。

该函数用于在支付网关的服务器中创建支付订单。

我需要的关于订单详细信息的数据存在于函数中(错误,数据)(参考代码)我需要将此数据发送回我的安卓应用程序。

我遇到的问题:我可以看到打印在 firebase 控制台日志中的数据,但它不会返回到我的 android 应用程序。

我的 Firebase 云功能:

const functions = require("firebase-functions");
exports.order = functions.https.onCall((amnt, response) => {

    const Ippopay = require('node-ippopay');

    var ippopay_instance = new Ippopay({
        public_key: 'pk_live_0WZhCNC5l7PJ',
        secret_key: 'sk_live_GXc8SLdxkBp',
      });
      
      ippopay_instance.createOrder({
        amount: amnt, 
        currency: 'DOLLAR',
        payment_modes: "cc,dc,nb,cheque",
        customer: {
            name: "Test",
            email: "test@gmail.com",
            phone: {
                country_code: "42",
                national_number: "4376543210"
            }
        }
    }, function  (err, data){
       
       return data.order.order_id;
    }); 
}); 

我的安卓客户端代码:

public class Payment extends AppCompatActivity implements IppoPayListener {

    Button pay;
    EditText amount;
    private FirebaseFunctions mFunctions;

    TextView order_data;
    String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
    }
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        pay=findViewById(R.id.pay_button);
        amount=findViewById(R.id.user_amount);
        order_data=findViewById(R.id.data_text);
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("PAY Button clicked", "yes");
                mFunctions = FirebaseFunctions.getInstance("us-central1");
               
                mFunctions.getHttpsCallable("order").call(5).continueWith(new Continuation<HttpsCallableResult, Object>() {
                    @Override
                    public Object then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                        HttpsCallableResult result=task.getResult();
                        if(result !=null)
                        {
                            data=result.getData().toString();
                            return result.getData().toString();

                        }
                        return null;
                    }
                 });
                order_data.setText(data);
                onPaymentClick();

            }
        });
    }

我是初学者,所以很可能会犯一些愚蠢的错误。 :)

【问题讨论】:

标签: android node.js firebase google-cloud-functions


【解决方案1】:

在您的云函数中,您不能只返回一个值,您需要使用response.json() 将其作为响应发送。改变这一行

return data.order.order_id;

到这里

response.json({ orderId: data.order.order_id });

有关示例,请参阅 docs

【讨论】:

猜你喜欢
  • 2021-06-21
  • 2019-10-11
  • 2019-09-17
  • 2020-05-28
  • 2018-05-24
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多