Firebase Function always finished with timeout

2019-02-25 19:13发布

问题:

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'

but the notification works.

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

回答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).

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:

  • Terminate HTTP Functions