Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetching from firestore and Firebase functions timeout

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()) })
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading