HTTPS可调用云函数未返回值 [英] Https callable cloud function not returning value

查看:227
本文介绍了HTTPS可调用云函数未返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flutter应用,我正在尝试从braintree获得客户随机数。根据 braintree文档,我在云函数中拥有此功能:

I have a Flutter app and I'm trying to get a client nonce from braintree. Per the braintree documentation, I have this in my cloud function:

exports.getClientNonce = functions.https.onCall(async (data, context) => {
    gateway.clientToken.generate({}, function (err, response) {
        if (err) {
            throw new functions.https.HttpsError('unknown', 'Error getting client nonce');
        } else {
            console.log(`token: ${response.clientToken}`);
            return response.clientToken;
        }
    });
});

然后,在Flutter应用中,我调用该函数(再次,我遵循插件说):

Then, in my Flutter app I call the function (again, I'm following what the plugin says):

try {
    HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
        functionName: 'getClientNonce',
    );

    dynamic result = await callable.call({});
    final value = result.data;  

    debugPrint('token: $value');

    var data = await BraintreePayment().showDropIn(
        nonce: value,
        amount: '2.0',
        enableGooglePay: false,
        inSandbox: true);
        print("Response of the payment $data");
    } on CloudFunctionsException catch (e) {
        debugPrint('An error occurred');
    } catch (e) {
        debugPrint('An error occurred');
    }
}

我尝试更改云功能,以便仅返回一个随机数(函数执行后立即显示),并且我的Flutter应用正确接收了该值(因此cloud函数可以正常通信)。在Firebase控制台中,我可以查看 console.log 指定的客户端随机数。但是该函数由于某种原因无法返回实际的客户随机数。 (应该应该是一些字符串哈希,其长度大于2000个字符)

I tried changing the cloud function so that it only returns a random number (as soon as the function is executed), and my Flutter app is correctly receiving the value (so the cloud function is communicating fine). And in my Firebase console, I am able to view the client nonce specified by console.log. But the function is for whatever reason unable to return the actual client nonce. (It should be should be some string hash that is >2000 characters long)

推荐答案

可调用函数需要从以下位置返回承诺返回值解析的函数回调的顶层。目前,您没有从顶层返回任何信息。现在的返回结果只是从传递给braintree API的内部回调函数返回一个值。这不会传播到最高级别。

The callable function needs to return a promise from the top-level of the function callback that resolves with the value to return. Right now, you're returning nothing from the top-level. The return you have now is just returning a value from the inner callback function that you pass to braintree API. This isn't going to propagate to the top level.

您需要做的是要么使用Braintree API版本返回API(如果存在),或承诺使用回调的现有呼叫。

What you need to do is either use a version of braintree API that returns an API (if one exists), or promisify the existing call that uses a callback.

另请参见此处的 3.节点样式回调:如何将现有的回调API转换为Promise?

See also "3. Node style callback" here: How do I convert an existing callback API to promises?

我尚未对此进行测试,但是如果您应用该模式,则通用格式将更像这样:

I have not tested this, but the general format if you apply that pattern will look more like this:

exports.getClientNonce = functions.https.onCall(async (data, context) => {
    return new Promise((resolve, reject) => {
        gateway.clientToken.generate({}, function (err, response) {
            if (err) {
                reject(new functions.https.HttpsError('unknown', 'Error getting client nonce'));
            } else {
                console.log(`token: ${response.clientToken}`);
                resolve(response.clientToken);
            }
        });
    });
});

这篇关于HTTPS可调用云函数未返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆