Search code examples
javascripttypescriptloggingfoalts

Cannot access Request Body in FoalTS hooks


I am trying to log request data from an API endpoint to ensure we have periodic backups for when the database crashes in between backup intervals. A logging hook has been created and attached to the controller where I access the Context variable as ctx. I have been able to access the request user through ctx.user but the body is undefined whenever I try to access it through ctx.request.body

To my knowledge vanilla JS requests have a .clone() method that clones the request because the request body is destroyed on use. My approach is to make a hook that logs the data in a file and writes to disk but so far the body showing up empty in the logging hook.

attempts to resolve:

  • copy context as in ctx2 = ctx and use ctx2 in the controller
  • copy context as in ctx2 = { ...ctx } and use ctx2in the controller
  • read node docs, no reference of a clone method.
  • set ctx.body to the body variable being used in the controller

is there anything being overlooked?


Solution

  • It looks like you're accessing the request body using the wrong key.

    The request body is accessible through ctx.request.body and not ctx.body. 👍