Cloud Functions for Firebase HTTP 超时 [英] Cloud Functions for Firebase HTTP timeout

查看:26
本文介绍了Cloud Functions for Firebase HTTP 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很接近这个.

我编写了一个云函数,它将从 Azure 令牌发送的信息用于自定义铸造 Firebase 令牌并将此令牌发送回客户端.

I have written a Cloud Function that takes information sent from an Azure token to custom mint a Firebase token and send this token back to the client.

令牌已正确创建,但未在我的 HTTP 请求中返回.

The token is created correctly, but isn't returned on my HTTP-request.

很遗憾,我的 Firebase 应用导致超时.

Unfortunately my Firebase app causes a timeout.

函数执行耗时 60002 毫秒,完成状态为:'timeout'

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

我真的无法理解为什么会这样,因此这篇文章.我的代码有问题,还是我调用 HTTP 请求错误?

I can't really wrap my head around why that is, hence this post. Is there something wrong with my code, or is it me that's calling the HTTP-request wrong?

这是我从 Firebase Functions 控制台获取的日志.

Here is the log I get from the Firebase Functions console.

这是我的代码

// 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,看起来像这样:

When I'm making this HTTP-request, all i'm sending in is a JSON that looks like this:

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

推荐答案

由 HTTP 请求触发的云函数需要通过 send(), redirect()end(),否则它们将继续运行并达到超时.

Cloud Functions triggered by HTTP requests need to be terminated by ending them with a send(), redirect(), or end(), otherwise they will continue running and reach the timeout.

来自关于 HTTP 触发器的文档的 终止 HTTP 函数部分:

From the terminate HTTP functions section of the documentation on HTTP triggers:

始终使用 send()redirect()end() 结束 HTTP 函数.否则,您的函数可能会继续运行并被系统强制终止.另请参阅同步、异步和承诺.

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.

使用 Node.js moment 检索和格式化服务器时间后a> 模块,date() 函数通过在 HTTP 响应中发送结果来结束:

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() 在响应中将令牌发回,例如:

So, within your code, you could send the token back in the response with send(), for example:

// ...
// 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;
});
// ...

这篇关于Cloud Functions for Firebase HTTP 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆