首页 > 解决方案 > Firebase 函数现在超时

问题描述

我阅读了其他关于 FIR 超时的 SO 帖子,其中一些是由于更新firebase-functionsfirebase-admin. 我更新到最新版本,甚至降级回原来的工作版本(git checkout)。

这些都不起作用。

我收到Function execution took 60002 ms, finished with status: 'timeout'任何 FIR 函数的错误(而请求在 Postman 中工作)

示例代码:

 exports.BSGetRequest = functions.https.onCall((url, context) => {
        console.log(url);

        const options = {
            method: 'GET',
            uri: url,
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Authorization': 'MY_PRIVATE_KEY'
            },
            json: true
        };

        return rp(options)
            .then(function (response) {
                console.log({ response });
                return repos;
            })
            .catch(function (err) {
                console.error({ err });
                return err
            });
    });

我怀疑当 Firebase Functions UI 也发生变化(在控制台中;我认为有一个重大更新)或者我的语法跟不上 Node 6 时,它就开始发生了。

更新:

FIR 函数再次开始工作,但我没有改变任何东西。结案。我希望这与我的付费计划订阅有关。

标签: javascriptfirebasegoogle-cloud-functions

解决方案


您的 UPDATED 错误消息可能会被抛出,因为response它不是对象并且没有body属性。

变量中可能有一些error您没有检查的消息。

我已经通过一些注释和调试对您的代码进行了一些分解,这应该可以帮助您深入了解事情的真相。

该请求将超时,因为它正在抛出错误并且没有通过promise.

exports.BSGetRequest = functions.https.onCall((url, context) => {
  const options = {
      url: url,
      headers: {
          'Content-Type' : 'application/json',
          'Accept': 'application/json',
          'Authorization': 'MY_PRIVATE_KEY'
      },
      json: true
  };

  return new Promise(function (fullfilled, rejected) {
    request.get(options, function (error, response, body) {
        const decoded = he.decode(JSON.stringify(response.body));

        if (!error && response.statusCode >= 200 && response.statusCode <= 300) {
            fullfilled(JSON.parse(decoded));
            return;
        }

        // What's in `error`?
        console.error({ error });

        // I'm guessing this is where the error is - What's in `response` - Is there a `body`?
        console.log({ response });

        const responseErrorMessage = he.decode(JSON.stringify(response.body));

        const responseError = new functions.https.HttpsError('invalid-argument', responseErrorMessage);

        rejected(responseError);
    })
  });
});

编辑:尝试抛出错误信息;

exports.BSGetRequest = functions.https.onCall((url, context) => {
  const options = {
      url: url,
      headers: {
          'Content-Type' : 'application/json',
          'Accept': 'application/json',
          'Authorization': 'MY_PRIVATE_KEY'
      },
      json: true
  };

  return new Promise(function (fullfilled, rejected) {
    request.get(options, function (error, response, body) {
        const decoded = he.decode(JSON.stringify(response.body));

        if (!error && response.statusCode >= 200 && response.statusCode <= 300) {
            fullfilled(JSON.parse(decoded));
            return;
        }

        // This should handle your error correctly.
        if (error) {
          const throwError = new functions.https.HttpsError('invalid-argument', error);

          rejected(throwError);
          return;
        }

        const responseErrorMessage = he.decode(JSON.stringify(response.body));

        const responseError = new functions.https.HttpsError('invalid-argument', responseErrorMessage);

        rejected(responseError);
    })
  });
});

推荐阅读