HTTP does not recognize the body

Viewed 84

this is my http code:

POST http://localhost:8000/register
host: localhost:8000
Content-Type: application/json

{
    "id":111111,
    "name":"example",
    "email":"example@example.com",
    "password":"example"
}

and the "/register" function is this (I am using react):

async register(ctx : RouterContext){
    await console.log(ctx.request.body());
   // const { value: {id,name, email, password} } = await ctx.request.body();

   // const user = await User.findOne({email})
   // if (user) {
   //     ctx.response.status = 422;
   //     ctx.response.body = {message : "Email is already in use"};
   //     return;
   // }
   // console.log(id,name, email, password);
}

I initially wanted to run what is commented out, to create a user. But when I do so I get a request error: ECONNREFUSED. So I just made it to console log what the body() actually is, but its empty. I have checked lots of websites and stack overflow posts yet no one has come across this bug where it doesn't identify my code. When I run it the way I just showed you I get this:

{ type: "json", value: Promise { <pending> } }
1 Answers

Please try:

async register(ctx: RouterContext){
   console.log(await ctx.request.body().value);
}
Related