Migration to module not working .json()
# workers-help
s
I'm converting my worker to module but the part where it gets posted data request.json() is no longer working. What am I doing wrong? I verified the payload is being sent. async fetch(request, env, ctx) { request.json() // return null }
k
Are you awaiting it?
s
yeah this is the code const { email } = await request.json();
Copy code
export default {
  async fetch(request, env, ctx) {
        response = await handleRegister(request);
    return response;

  },
};

async function handleRegister(request) {
  console.log("start");
  const { email } = await request.json();
  console.log("after");
  return new Response(JSON.stringify({ message: "User created" }), { status: 201 });
}
the console "start" runs but then the error happens at the .json() and "after" doesn't log.
j
What is the error exactly that you’re seeing? Are you certain you’re passing valid json as your POST body?
s
This doesn't seem to be showing any data in it either. Yeah I was using this worker and wanted to switch from KV to D1 and it needed the module migration. The exact error was Unexpected end of JSON input. That's because it's null. All of the request except the request.url seem to be null. request.headers.get('Content-Type') request.headers.get('Origin')
I'm getting closer. It was the OPTION request that was ending the connection. Yahoo