Https callable cloud function not returning value

后端 未结 1 1242
醉酒成梦
醉酒成梦 2021-01-29 03:05

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.getClientNo         


        
相关标签:
1条回答
  • 2021-01-29 03:31

    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.

    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.

    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);
                }
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题