【问题标题】:Firebase Functions not returning response in FlutterFirebase 函数未在 Flutter 中返回响应
【发布时间】:2020-01-22 21:34:39
【问题描述】:

我有一个调用 Stripe API 来创建用户的 firebase 函数,效果很好。但是,我无法从 Flutter 应用程序中的函数返回有效响应。无论我尝试什么,响应数据始终为空,即使函数返回响应,而不是错误。此代码与文档中的示例几乎相同。谁能发现我哪里出错了?

功能

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;
    }

【问题讨论】:

    标签: firebase flutter google-cloud-functions


    【解决方案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;
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 1970-01-01
      • 2021-01-15
      • 2017-09-21
      • 1970-01-01
      • 2021-10-02
      • 2021-03-10
      • 1970-01-01
      相关资源
      最近更新 更多