Функции Firebase не возвращают ответ в Flutter - PullRequest
0 голосов
/ 22 сентября 2019

У меня есть функция firebase, которая вызывает API Stripe для создания пользователя, что прекрасно работает.Тем не менее, я не могу получить правильный ответ от функции в моем приложении 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;
    }

1 Ответ

1 голос
/ 22 сентября 2019

Я думаю, что вы должны вернуть цепочку Обещаний следующим образом:

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;
  });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...