'Koa cannot get the value of the property inside ctx.request-body

Koa cannot get the value of the property inside ctx.request-body

Project koa is generated by koa-generator

Routing section related code Either require('koa-body') or require('koa-bodyparser')

  console.log("ctx")
  console.log(ctx.request.body)
  console.log(ctx.request.body.type)
})

The three console.log prints are

ctx

{
account:'root',
password: 'test',
type:0
}

undefined

I can get the object inside the ctx.requisition.body and print it out, but ctx.request.body.type is undefined How to get 'ctx.requisition.body.account' or 'ctx.requisition.body.password' ?



Solution 1:[1]

Maybe if you do this

const myObj = JSON.parse(ctx.request.body)
console.log(myObj.type)

You'll get ctx.request.body.type

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ruben Cubillos Mujica