javascript - Cloud Functions for Firebase HTTP 超时

标签 javascript firebase google-cloud-functions

我很喜欢这个。

我编写了一个 Cloud Functions,它获取从 Azure token 发送的信息来自定义类型转换一个 Firebase token 并将此 token 发送回客户端。

token 已正确创建,但未在我的 HTTP 请求中返回。

不幸的是,我的 Firebase 应用导致超时。

Function execution took 60002 ms, finished with status: 'timeout'

我真的想不通为什么会这样,因此写了这篇文章。是我的代码有问题,还是我调用了错误的 HTTP 请求?

这是我从 Firebase Functions 控制台获得的日志。

Firebase functions log

这是我的代码

// Create a Firebase token from any UID
exports.createFirebaseToken = functions.https.onRequest((req, res) => {

  // The UID and other things we'll assign to the user.
  const uid = req.body.uid;
  const additionalClaims = {
    name: req.body.name,
    email: req.body.email
  };

  // Create or update the user account.
  const userCreationTask = admin.auth().updateUser(uid, additionalClaims).catch(error => {

    // If user does not exists we create it.
    if (error.code === 'auth/user-not-found') {
      console.log(`Created user with UID:${uid}, Name: ${additionalClaims.name} and e-mail: ${additionalClaims.email}`);
      return admin.auth().createUser({
        uid: uid,
        displayName: displayName,
        email: email,
      });
    }
    throw error;
    console.log('Error!');
  });

  // Wait for all async tasks to complete, then generate and return a custom auth token.
  return Promise.all([userCreationTask]).then(() => {
    console.log('Function create token triggered');
    // Create a Firebase custom auth token.
    return admin.auth().createCustomToken(uid, additionalClaims).then((token) => {
      console.log('Created Custom token for UID "', uid, '" Token:', token);
      return token;
    });
  });
});

当我发出这个 HTTP 请求时,我发送的只是一个 JSON,如下所示:

parameters = [
    "uid" : id,
    "email" : mail,
    "name" : name
]

最佳答案

由 HTTP 请求触发的 Cloud Functions 需要以 send()redirect()end() 结尾来终止它们>,否则它们将继续运行并达到超时。

来自terminate HTTP functions section of the documentation on HTTP triggers :

Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might to continue to run and be forcibly terminated by the system. See also Sync, Async and Promises.

After retrieving and formatting the server time using the Node.js moment module, the date() function concludes by sending the result in the HTTP response:

const formattedDate = moment().format(format);
console.log('Sending Formatted date:', formattedDate);
res.status(200).send(formattedDate);

因此,在您的代码中,您可以使用 send() 在响应中发回 token ,例如:

// ...
// Create a Firebase custom auth token.
return admin.auth().createCustomToken(uid, additionalClaims).then((token) => {
  console.log('Created Custom token for UID "', uid, '" Token:', token);
  res.status(200).send(token);
  return token;
});
// ...

关于javascript - Cloud Functions for Firebase HTTP 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47154257/

相关文章:

javascript - 如何在 angularjs 中将 $scope 变量与文本编辑器绑定(bind)

javascript - 如何更改 AJAX 驱动页面中 <input> 的名称字段?

node.js - Firebase函数 Stripe 错误

javascript - Firebase 函数 onWrite 未被调用

javascript - 如果服务器关闭,ajax 调用的 jQuery 错误回调

swift - Firebase 数据库不存储用户信息

javascript - Firebase - 对象与数组并返回 .key()

firebase - Android Studio 更新后 Google 和 Firebase 依赖项未解决

javascript - Firebase 的云函数 - 使用 Promise 循环

javascript - 创建文本节点会导致 [object HTMLSpanElement],为什么?