适用于Firebase HTTP超时的云功能 [英] Cloud Functions for Firebase HTTP timeout

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

问题描述

我真的很接近这个。



我编写了一个云功能,它将从 Azure 令牌发送的信息转换为自定义模板Firebase令牌并将此令牌发送回客户端。



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



不幸的是我的Firebase应用程序会导致超时。


函数执行耗时60002 ms,状态为:'timeout'


我无法真正理解为什么会这样,因此这篇文章。我的代码是否有问题,或者是我调用HTTP请求错误了吗?



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





这是我的代码

  //从任何UID创建Firebase令牌
exports.createFirebaseToken = functions.https.onRequest((req,res)=> {

// UID和我们将分配给用户的其他东西。
const uid = req.body.uid;
const additionalClaims = {
name:req.body。 name,
email:req.body.email
};

//创建或更新用户帐户。
const userCreationTask = admin.auth()。updateUser (uid,additionalClaims).catch(error => {

//如果用户不存在,我们创建它。
if(error.code ==='auth / user-not -found'){
consol e.log(`使用UID创建的用户:$ {uid},名称:$ {additionalClaims.name}和电子邮件:$ {additionalClaims.email}`);
返回admin.auth()。createUser({
uid:uid,
displayName:displayName,
email:email,
});
}
抛出错误;
console.log('错误!');
});

//等待所有异步任务完成,然后生成并返回自定义身份验证令牌。
返回Promise.all([userCreationTask])。then(()=> {
console.log('Function create token triggered');
//创建Firebase自定义身份验证令牌。
返回admin.auth()。createCustomToken(uid,additionalClaims).then((token)=> {
console.log('为UID创建自定义令牌'',uid,''令牌:',token);
返回令牌;
});
});
});

当我发出这个HTTP请求时,我发送的所有内容都是 JSON 看起来像这样:

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


解决方案

由HTTP请求触发的云函数需要以 send(),<$结束它们来终止c $ c> redirect()或 end(),否则它们将继续运行并达到超时。



来自终止HTTP文档的HTTP功能部分触发器


始终以结束HTTP函数发送() redirect(),或 end()。否则,您的功能可能会继续运行并被系统强行终止。另请参阅同步,异步和承诺



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

  const formattedDate = moment()。format(format); 
console.log('发送格式化日期:',formattedDate);
res.status(200).send(formattedDate);


因此,在您的代码中,您可以重新发送令牌回复 send(),例如:

  //。 .. 
//创建Firebase自定义身份验证令牌。
返回admin.auth()。createCustomToken(uid,additionalClaims).then((token)=> {
console.log('为UID创建自定义令牌'',uid,'令牌: ',token);
res.status(200).send(token);
返回令牌;
});
// ...


I'm so close with this one.

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.

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

Unfortunately my Firebase app causes a timeout.

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

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?

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

Here's my code

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

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
]

解决方案

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.

From the 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);

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

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

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