[英]Cloud Functions for Firebase HTTP timeout
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. 我编写了一个Cloud Function,它将从Azure令牌发送的信息转换为自定义的Firebase令牌并将此令牌发送回客户端。
The token is created correctly, but isn't returned on my HTTP-request. 令牌已正确创建,但不会在我的HTTP请求中返回。
Unfortunately my Firebase app causes a timeout. 很遗憾,我的Firebase应用会导致超时。
Function execution took 60002 ms, finished with status: 'timeout'
函数执行花了60002毫秒,完成状态:'超时'
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?
我的代码有问题,还是我调用HTTP请求错了?
Here is the log I get from the Firebase Functions console. 这是我从Firebase Functions控制台获得的日志。
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: 当我发出这个HTTP请求时,我发送的所有内容都是一个如下所示的
JSON
:
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. 由HTTP请求触发的云功能需要通过以
send()
, redirect()
或end()
结束来终止,否则它们将继续运行并达到超时。
From the terminate HTTP functions section of the documentation on HTTP triggers : 从HTTP触发器文档的终止HTTP功能部分 :
Always end an HTTP function with
send()
,redirect()
, orend()
.始终使用
send()
,redirect()
或end()
结束HTTP功能。 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, thedate()
function concludes by sending the result in the HTTP response:在使用Node.js
moment
模块检索和格式化服务器时间之后,date()
函数通过在HTTP响应中发送结果来结束: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: 因此,在您的代码中,您可以使用
send()
将响应send()
回响应,例如:
// ...
// 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;
});
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.