首页 > 解决方案 > FirebaseFunctions.getinstance() 未完成

问题描述

我正在使用 FirebaseFunctions 以及https://github.com/voltrue2/in-app-purchase来验证购买。我无法将数据发送回我的客户。在我的函数日志中,一切顺利,我得到了我想要的结果,但据我了解,在使用 Firebase 的 onCall https 触发器时,您必须返回一个承诺才能将数据返回给客户端。我的 oncall 触发器的代码在这里。

var iap = require('in-app-purchase');
var purchaseData;
const receipt = data;


iap.config({
    googlePublicKeyPath: 'acualpublickey' ,
    googleAccToken: 'actualtoken',
    googleRefToken: 'actualtoken', 
    googleClientID: 'actualID', 
    googleClientSecret: 'actual seceret', 




});


iap.setup()
        .then(() => {
            console.log("receipt: ", receipt)


            return iap.validate(receipt).then(onSuccess).catch(onError);





        }).catch((error) => {
            console.log("error setup: " + error);
            throw error;
});




function onSuccess(validatedData) {

    var options = {
        ignoreExpired: true // purchaseData will NOT contain exipired subscription items
    };
    purchaseData = iap.getPurchaseData(validatedData, options);

    console.log("PurchseData : ", purchaseData);
    console.log("ValidatedData : ", validatedData);
    return purchaseData;
}


function onError(error) {
    console.log("error onError: " + error);
    throw error;
}

它被调用的函数就是这个

 void receiptJSONvalidation(String receipt){
    if (receipt != null) {
        try {


            JSONObject jsonObject = new JSONObject(receipt);

            Task<Object> validatedReceipt =  validation(jsonObject);

            validatedReceipt.addOnCompleteListener(this, new OnCompleteListener<Object>() {
                @Override
                public void onComplete(@NonNull Task<Object> task) {
                    if (!task.isSuccessful()) {
                        Exception e = task.getException();
                        if (e instanceof FirebaseFunctionsException) {
                            FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                            FirebaseFunctionsException.Code code = ffe.getCode();
                            Object details = ffe.getDetails();

                            errorAlert("task unsuccessful: " + details.toString());
                        } else {
                            errorAlert("Error onComplete: " + e.getLocalizedMessage());
                        }

                        // ...
                    } else{
                        Gson gson = new Gson();

                        String taskResult = gson.toJson(task.getResult());
                        errorAlert("taskResult: " + taskResult);
                    }
                }
            });


        } catch (JSONException e) {
            errorAlert("Error Json: " + e.getLocalizedMessage());
        }

    } else {
        errorAlert("You are not subscribed. Please purchase a subscription.");
    }
}

验证方法是这样的

private Task<Object> validation(JSONObject receipt){
    mFunctions = FirebaseFunctions.getInstance();
    return mFunctions.getHttpsCallable("receiptValidation").call(receipt)
            .continueWith(new Continuation<HttpsCallableResult,Object>() {
                public Object then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    Object result = task.getResult().getData();

                    return result;
                }
            });
}

当我得到 taskResult 和 result 时,我的日志有我需要的实际数据,但我得到了 null。我尝试遵循这些功能的文档,但我仍然没有得到需要的东西。我的应用程序似乎也存在问题,不允许 firebase 功能完成。有时结果会在函数结束之前弹出 null 。该函数会说它花了大约 8 秒。然而,警报弹出的时间更短。

标签: firebasepromisetaskgoogle-cloud-functions

解决方案


推荐阅读