Как использовать полосу вызова asyn c для получения токена и возврата в вызываемой функции firebase? - PullRequest
0 голосов
/ 09 мая 2020

Я не очень хорошо знаком с node.js и пытаюсь реализовать эту функцию без вызываемых, и она работает нормально. Но я решил использовать вызываемую функцию вместо http-вызова. Как вы можете видеть здесь:

Это работает нормально:

exports.getStripeEphemeralKeys = functions.https.onRequest((req, res) => {
    const apiVersion = req.body.apiVersion;
    const customerId = req.body.customerId;
    console.log("apiVersion == "+ apiVersion);
    console.log("customerId == "+ customerId);
    if (!apiVersion) {
      console.log('I did not see any api version');
      res.status(400).end();
      return;
    }
    stripe.ephemeralKeys.create(
      {customer: customerId},
      {apiVersion: apiVersion}
    ).then((key) => {
       console.log("Ephemeral key: " + key);
       res.status(200).json(key);
       return;
    }).catch((err) => {
      console.log('stripe version is ' + stripe_version + " and customer id is " + customerId + " for key: " + stripe_key + " and err is " + err.message );
      res.status(500).json(err);
      return;
    });
  });

Но теперь я решил изменить его на вызываемый. Я использовал функцию обратного вызова n, которой нет в этом вопросе, но она не сработала. Поэтому я решил использовать asyn c, но все еще не работает. В основном мне нужно дождаться ответа полосы, и как только я получу свой ответ, мне нужно отправить его обратно в свое приложение iOS. Я ценю, если кто-нибудь может мне помочь с этим:

async function callStripe(apiVersion, customerId) {
    if (!apiVersion) {
        console.log('I did not see any api version');
        return {error : "api version is not correct"};
    }
    stripe.ephemeralKeys.create(
        {customer: customerId},
        {apiVersion: apiVersion}
      ).then((key) => {
         console.log("Ephemeral key: " + key);
         return {key : key};
      }).catch((err) => {
        console.log('stripe version is ' + apiVersion + " and customer id is " + customerId + " and err is " + err.message);
        return {error : 'stripe version is ' + apiVersion + " and customer id is " + customerId + " and err is " + err.message};
      });
}

exports.getStripeEphemeralKeysCallable = functions.https.onCall((data, context) => {
    const apiVersion = data.apiVersion;
    const customerId = data.customerId;
    console.log("apiVersion == "+ apiVersion);
    console.log("customerId == "+ customerId);
    let ephemeralKey = await callStripe(apiVersion, customerId);
    return {
        key : ephemeralKey
    }
});

С приведенным выше кодом мой код не развертывается в firebase, и я получаю эту ошибку:

  90:34  error  Parsing error: Unexpected token callStripe

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/Bernard/.npm/_logs/2020-05-09T17_36_30_857Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1
...