gpt4 book ai didi

javascript - Firebase 函数。https.onCall 返回 null

转载 作者:行者123 更新时间:2023-12-02 21:44:22 27 4
gpt4 key购买 nike

我有以下用 node.js 编写的 firebase 云函数,我从 Android 应用程序调用该函数。该函数工作正常,但在尝试在 Android 中获取结果时得到 null。我需要返回到我的 Android 应用程序 console.log(result.data); 行,但该行在 firebase 函数完全完成后调用,因此我的 Android 应用程序为 null。

index.js

'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const subcheck = require('./subcheck');
admin.initializeApp();


exports.subcheck = functions.https.onCall((data, context) => {
subcheck.verifySubscription(data, context);
});

这是我的 subcheck.js

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const key = require('./service-account-key.json'); // JSON key file
const {google} = require('googleapis');

const authClient = new google.auth.JWT({
email: key.client_email,
key: key.private_key,
scopes: ["https://www.googleapis.com/auth/androidpublisher"]
});

const playDeveloperApiClient = google.androidpublisher({
version: 'v3',
auth: authClient
});

//admin.initializeApp(functions.config().firebase);

exports.verifySubscription = function(data, context) {
const skuId = data.sku_id;
const purchaseToken = data.purchase_token;
const packageName = data.package_name;

return authClient.authorize()
.then(function(result) {
return playDeveloperApiClient.purchases.subscriptions.get({
packageName: packageName,
subscriptionId: skuId,
token: purchaseToken
}).then(function(result) {
if (result.status === 200) {
console.log(result.data);
return {
data: result.data,
status: 200,
message: "Verified Subscription"
};
} else {
console.log("Failed to verify subscription, Try again!");
return {
data: result.data,
status: 500,
message: "Failed to verify subscription, Try again!"
};
}
})
.catch(function(error) {
console.log(error);
});

})
.catch(function(error) {
console.log(error);
});

}

这是我的 firebase 函数控制台日志:

enter image description here

我尝试使用以下方法在 Android 中获取结果:

    private Task<String> checkUserSubscribed(PurchaseHistoryRecord purchase) {
Map<String, Object> mapUserPurchase = new HashMap<>();
mapUserPurchase.put("sku_id", purchase.getSku());
mapUserPurchase.put("purchase_token", purchase.getPurchaseToken());
mapUserPurchase.put("package_name", "xxxxxxx");

return mFirebaseFunctions
.getHttpsCallable("subcheck")
.call(mapUserPurchase)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
String result = (String) task.getResult().getData();
return result;
}
}).addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
String result = task.getResult();
Toast.makeText(MainActivity.this, "func results: " + result, Toast.LENGTH_SHORT).show();
}
});
}

最佳答案

问题很可能来自以下事实:您没有返回对 verifySubscription() 函数的调用结果:

exports.subcheck = functions.https.onCall((data, context) => {
subcheck.verifySubscription(data, context);
});

你应该这样做:

exports.subcheck = functions.https.onCall((data, context) => {
return subcheck.verifySubscription(data, context);
});
<小时/>

此外,您可能应该按如下方式重新组织 Promise 链,以避免嵌套对 then() 的调用,请参阅更多详细信息/说明 here .

exports.verifySubscription = function(data, context) {
const skuId = data.sku_id;
const purchaseToken = data.purchase_token;
const packageName = data.package_name;

return authClient.authorize()
.then(result => {
return playDeveloperApiClient.purchases.subscriptions.get({
packageName: packageName,
subscriptionId: skuId,
token: purchaseToken
})
})
.then(result => {
if (result.status === 200) {
console.log(result.data);
return {
data: result.data,
status: 200,
message: "Verified Subscription"
};
} else {
console.log("Failed to verify subscription, Try again!");
return {
data: result.data,
status: 500,
message: "Failed to verify subscription, Try again!"
};
}
})
})
.catch(function(error) {
console.log(error);
});

}
<小时/>

最后,您可以看看 documentation关于可调用云函数中的错误处理。您可以抛出 functions.https.HttpsError 的实例,而不是返回 {data: result.data,status: 500, ...};

关于javascript - Firebase 函数。https.onCall 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60296909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com