Scheduled Function runs 3 times in a row

I’ve walked through this tutorial and have successfully setup a scheduled function to run every minute with the code below.

export default async (req) => {
  const { next_run } = await req.json();
  console.log("Received event! Next invocation at:", next_run);  
};

export const config = {
  schedule: "* * * * *",
};

One weird catch though. Instead of the function running once, it fires three times. After some research, I found this thread which suggested that the handler function fired multiple times because a status code of 200 wasn’t returned to indicate that the function completed without error. Makes sense. So I added that…

export default async (req) => {
  const { next_run } = await req.json();
  console.log("Received event! Next invocation at:", next_run);  

  return {
    statusCode: 200,
  };
};

export const config = {
  schedule: "* * * * *",
};

Unfortunately returning a status code of 200 throws a breaking error, causing the function to not fire correctly. Here’s the error it throws:

Oct 23, 11:22:02 PM: 6e6e80cc ERROR  Invoke Error 	{"errorType":"Error","errorMessage":"Function returned an unsupported value. Accepted types are 'Response' or 'undefined'","stack":["Error: Function returned an unsupported value. Accepted types are 'Response' or 'undefined'","    at Runtime.handler (file:///var/task/___netlify-bootstrap.mjs:1869:9)","    at async Runtime.handleOnceStreaming (file:///var/runtime/index.mjs:1206:26)"]}

The scheduled function docs make no mention of needing to return a 200 code. And when I do, unfortunately the function won’t run correctly. Any ideas or suggestions?

Thanks for taking the time!

When using the Netlify Functions 2.0, use the standard web API Response:

export default async (req) => {
  const { next_run } = await req.json();
  console.log("Received event! Next invocation at:", next_run);  

  return new Response("Ok");
};

export const config = {
  schedule: "* * * * *",
};
1 Like

That did the trick - thank you, Matt!

glad to hear it. Thanks for writing back in!