FirebaseFunctions.getinstance()未完成
我使用FirebaseFunctions以及验证购买。我无法将数据发送回我的客户。在我的函数日志中,一切进展顺利,我得到了我想要的结果,但据我所知,在使用Firebase的onCall https触发器时,为了将数据返回到客户端,您必须返回一个承诺。我的oncall触发器的代码在这里FirebaseFunctions.getinstance()未完成,firebase,promise,task,google-cloud-functions,Firebase,Promise,Task,Google Cloud Functions,我使用FirebaseFunctions以及验证购买。我无法将数据发送回我的客户。在我的函数日志中,一切进展顺利,我得到了我想要的结果,但据我所知,在使用Firebase的onCall https触发器时,为了将数据返回到客户端,您必须返回一个承诺。我的oncall触发器的代码在这里 var iap = require('in-app-purchase'); var purchaseData; const receipt = data; iap.config({ googlePubl
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.");
}
}
void receiptjson验证(字符串接收){
如果(收据!=null){
试一试{
JSONObject JSONObject=新JSONObject(接收);
任务validatedReceipt=验证(jsonObject);
validatedReceipt.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
如果(!task.issusccessful()){
异常e=task.getException();
if(FirebaseFunctionsException的实例){
FirebaseFunctionsException ffe=(FirebaseFunctionsException)e;
FirebaseFunctionsException.Code=ffe.getCode();
对象详细信息=ffe.getDetails();
errorAlert(“任务未成功:+details.toString());
}否则{
errorAlert(“完成时出错:+e.getLocalizedMessage());
}
// ...
}否则{
Gson Gson=新的Gson();
字符串taskResult=gson.toJson(task.getResult());
errorAlert(“taskResult:+taskResult”);
}
}
});
}捕获(JSONException e){
errorAlert(“错误Json:+e.getLocalizedMessage());
}
}否则{
errorAlert(“您未订阅。请购买订阅”);
}
}
验证方法如下所示
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;
}
});
}
私有任务验证(JSONObject接收){
mFunctions=FirebaseFunctions.getInstance();
返回mFunctions.GetHttpScalable(“receiptValidation”).call(收据)
.continueWith(新的continueWith(){
然后公共对象(@NonNull任务)引发异常{
//此延续在成功或失败时运行,但如果任务
//失败,则getResult()将引发异常,该异常将
//向下传播。
对象结果=task.getResult().getData();
返回结果;
}
});
}
当我同时得到taskResult和result时,我得到的是null,而我的日志中有我需要的实际数据。我试图遵循这些函数的文档,但仍然没有得到所需的内容。此外,我的应用程序似乎也存在问题,无法完成firebase功能。有时,在函数结束之前,结果会弹出null。函数会说大约花了8秒。然而,警报弹出的时间要短得多。“…不允许firebase功能完成”肯定与不返回所需承诺一致。您认为应该从哪个函数返回承诺?如果我必须猜测的话,应该是
mFunctions.GetHttpScalable(…).call(…).continueWith()
需要被承诺,并且承诺需要包括result.addOnCompleteListener(…)
,您将从中解析/拒绝新承诺()
wrapper(返回)。因此,当前在receiptJSONvalidation()
中的大部分代码将被重新组合到validation()
,留下一般形式的代码返回验证(jsonObject)代码>。您可能仍然需要if/try/catch/else
结构-只需确保所有分支都返回承诺即可。