Home > Software engineering >  Fetching from firestore and Firebase functions timeout
Fetching from firestore and Firebase functions timeout

Time:02-10

I have a simple function that I am testing locally on the emulator.

    exports.sendNotification = functions.https.onRequest(() => {
  firebase
    .firestore()
    .collection("Users")
    .get()
    .then((snapshot) => {
      snapshot.docs.forEach((doc) => {
        console.log(doc.id, doc.data());
      });
    });
});

When I execute the function, it works fine, but shortly after I get this message:

     functions: Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
>  C:\Users\myusername\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:618
>              throw new Error("Function timed out.");
>              ^
>
>  Error: Function timed out.
>      at Timeout._onTimeout (C:\Users\myusername\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:618:19)
>      at listOnTimeout (node:internal/timers:557:17)
>      at processTimers (node:internal/timers:500:7)

my expectation was that the function would just end normally since its at the end of the function, is this message saying that I need to do something to indicate the function has finished? Do I need to change something especially when pushing to production?

CodePudding user response:

You must terminate a HTTP function by sending back a response otherwise it'll run until it times out. Try refactoring your code like this:

exports.sendNotification = functions.https.onRequest((req, res) => {
  // add req, res in functions parameters             ^^^
  return firebase
    .firestore()
    .collection("Users")
    .get()
    .then((snapshot) => {
      snapshot.docs.forEach((doc) => {
        console.log(doc.id, doc.data());
      });
      // Return response
      return res.json({ data: snapshot.docs.map(d => d.data()) })
    });
});
  • Related