Ad

Firebase Cloud Function Always Returns Null

I'm trying to use the following function to interact with the Stripe API - it seems to work fine as the key object is logged correctly in the console and appears as expected.

exports.createEphemeralKey = functions.https.onCall((data,context) => {
  const stripe_version = data.api_version;
  const customerId = data.customer_id;
  if (!stripe_version) {
    throw new functions.https.HttpsError('missing-stripe-version','The stripe version has not been provided.');
  }
  if (customerId.length === 0) {
    throw new functions.https.HttpsError('missing-customerID','The customer ID has not been provided.');
  }
  return stripe.ephemeralKeys.create(
      {customer: customerId},
      {stripe_version: stripe_version}
  ).then((key) => {
    console.log(key);
    return key;
  }).catch((err) => {
      console.log(err);
      throw new functions.https.HttpsError('stripe-error', err);
  });
});

However, when I call this function from my Swift iOS App, the result?.data is always nil.

let funcParams = ["api_version": apiVersion, "customer_id": "......"]
    functions.httpsCallable("createEphemeralKey").call(funcParams) { (result, error) in
        if let error = error as NSError? {
            print(error)
            completion(nil,error)
        }
        if let json = result?.data as? [String: AnyObject] {
            print("success")
            print(json)
            completion(json, nil)
        } else {
            print("fail")
            print(result?.data)
        }
    }
Ad

Answer

It is probably because you don't return "data that can be JSON encoded" (see the doc). The following should work:

....
).then((key) => {
  console.log(key);
  return {key: key};
})
....
Ad
source: stackoverflow.com
Ad