Trying to add a cron job to my worker.

I have an existing worker that uses Hono to expose some API endpoints:
const app = new Hono();
...
export default app;
const app = new Hono();
...
export default app;
I am trying to add a cron job to this worker but I am continuously getting either a "Handler does not export a scheduled() function" error or a, "worker.fetch is not a function" error. I am defining my scheduled.ts worker like this:
export default {

  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// ...
  },

  async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
  // ...
  }
export default {

  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// ...
  },

  async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
  // ...
  }
and then updating my main index.ts export like this:
export default {
    app,
    scheduled,
    fetch
}
export default {
    app,
    scheduled,
    fetch
}
But at runtime I am getting the error: Error: Handler does not export a fetch() function.
2 Replies
Cyb3r-Jak3
Cyb3r-Jak36d ago
I have something like
const handler = {
fetch: app.fetch,
scheduled(_: ScheduledEvent, env: ENV, ctx: ExecutionContext) {
<event>
},
}
export default handler
const handler = {
fetch: app.fetch,
scheduled(_: ScheduledEvent, env: ENV, ctx: ExecutionContext) {
<event>
},
}
export default handler
CleverPatrick
CleverPatrickOP4d ago
Thanks! This worked for me.

Did you find this page helpful?