FirebaseFunctions.getinstance()未完成

时间:2018-08-23 21:53:11

标签: firebase promise task google-cloud-functions

我正在使用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秒钟。但是,警报弹出的时间更少了。

0 个答案:

没有答案