Firebase函数未在Flutter中返回响应

时间:2019-09-22 17:46:33

标签: firebase flutter google-cloud-functions

我有一个firebase函数,该函数调用Stripe API创建一个用户,效果很好。但是,我无法从Flutter应用程序中的函数获得有效的响应。无论我如何尝试,即使函数返回响应而不是错误,响应数据也始终为null。这段代码与文档中的示例几乎相同。谁能发现我要去哪里错了?

功能

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  stripe.accounts.create({
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
    .then((account) => {
      console.log(`Stripe account: ${account.id}`);
      return {
        stripeId: account.id
      };
    })
    .catch((err) => {
      console.log(`Err: ${JSON.stringify(err)}`);
      return null;
    });
});
final HttpsCallable callable =
        CloudFunctions.instance.getHttpsCallable(functionName: 'stripeSignup');

    try {
      final HttpsCallableResult res = await callable.call(<String, dynamic>{
        'first_name': firstname,
        'last_name': lastname,
        'dob': dob,
        'email': email,
        'phone': phone,
      });

      print('Stripe response - ${res.data}');
      String stripeId = res.data['stripeId'];
      return stripeId;
    } on CloudFunctionsException catch (e) {
      return null;
    }

1 个答案:

答案 0 :(得分:1)

我认为您应该按如下方式归还您的Promises链:

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  return stripe.accounts.create({    // <--  Note the return here
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
  .then((account) => {
    console.log(`Stripe account: ${account.id}`);
    return {
      stripeId: account.id
    };
  })
  .catch((err) => {
    // Here you should handle errors as explained in the doc
    // https://firebase.google.com/docs/functions/callable#handle_errors
    console.log(`Err: ${JSON.stringify(err)}`);
    return null;
  });
});