Firebase 函数总是以超时结束

发布时间:2023-03-02 / 作者:清心寡欲
本文介绍了firebase 函数总是以超时结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 firebase 功能通过云消息创建通知.但我总是收到这个错误:

i am using firebase function for creating notifications with cloud messaging. But i am always getting this Error:

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

但通知有效.

这是我在 index.js 中使用的代码:

This is the code i am using in index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


exports.SendNotification = functions.https.onRequest((req, res) => {  

  var payload = {
      notification: {
         title: "this is a test",
         body: req.rawBody.toString('utf8')
      }
  }

  return admin.messaging().sendToTopic("all", payload);

});

我必须实施响应吗?什么时候,我该怎么做?

Do i have to implement a response? When, how do i do that?

J3nsis

推荐答案

一个 HTTPS 触发的云函数在它向调用者发送响应时结束.由于您的代码从不发送响应,因此代码会一直运行直到其配置的超时(默认为 1 分钟).

A HTTPS-triggered Cloud Function ends when it sends a response to its caller. Since your code never sends a response, the code keep running until its configured timeout (which is 1 minute by default).

要在函数完成后正确终止函数,请在 FCM 调用完成后发回结果:

To properly terminate the function when it's done, send a result back after the FCM call completes:

admin.messaging().sendToTopic("all", payload).then(() => {
  res.status(200).send("ok");
}).catch((err) => {
  res.status(500).send(err);
});

我建议阅读文档中的这一部分:

I recommend reading this section in the docs:

  • 终止 HTTP 函数

这篇关于Firebase 函数总是以超时结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]Firebase Function always finished with timeout


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。