我编写了一个云功能,它将从 Azure 令牌发送的信息转换为自定义模板Firebase令牌并将此令牌发送回客户端。
令牌已正确创建,但未在我的HTTP请求中返回。
不幸的是,我的Firebase应用会导致超时。
函数执行耗时60002毫秒,完成状态:'超时'
我无法真正理解为什么会这样,因此这篇文章。我的代码有问题,还是我调用HTTP请求错了?
以下是我从Firebase Functions控制台获取的日志。
这是我的代码
// 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
]
答案 0 :(得分:7)
由HTTP请求触发的云功能需要以send()
,redirect()
或end()
结束,否则它们将继续运行并达到超时。
来自terminate HTTP functions section of the documentation on HTTP triggers:
始终使用
send()
,redirect()
或end()
结束HTTP功能。否则,您的功能可能会继续运行并被系统强行终止。另请参阅Sync, Async and Promises。使用Node.js
moment
模块检索并格式化服务器时间后,date()
函数通过在HTTP响应中发送结果来结束:const formattedDate = moment().format(format); console.log('Sending Formatted date:', formattedDate); res.status(200).send(formattedDate);
因此,在您的代码中,您可以使用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;
});
// ...