0

I am trying to console.log("ctx : "+ ctx.request.body); To view what is in the JSON that received when tried it shows ctx : [object Object].

But if I console.log("ctx : ", ctx.request.body); like this it prints the JSON correctly without + in console.log(). I want to know what is the reason behind this logic

2 Answers2

0

When you use someString + someObject, when the result is a new string. The object must be converted to a string first before the new string can be created.

Whenever an object is cast to a string in Javascript, the result is [object Object].

To see this behavior you can also.

const obj = {};
const str = obj.toString(); // convert to string

console.log(str);
Evert
  • 93,428
  • 18
  • 118
  • 189
0

If you want to have a formatted json output you also can do the following:

console.log("ctx:");
console.log(JSON.stringify(ctx.request.body, null, 2);
Sebastian Hildebrandt
  • 2,661
  • 1
  • 14
  • 20