My server gets a response from another server with a document from a mongodb database with a body similar to
{
messageDetails: {
body: "hello"
},
customerDetails: {
cstmrFirstName: 'Random',
cstmrLastName: 'Name',
},
}
But the response with the body is being logged to the console as:
{
messageDetails: [Object],
customerDetails: [Object],
}
How can I log the full object to the console with each object’s properties?
4
Answers
const util = require(‘util’)
console.log(util.inspect(objectName, {showHidden: false, depth: null, colors: true}))
Try
console.log(JSON.stringify(body))
Checkout MDN doc JSON.stringify() for more details
You can use console.dir. To print with unlimited depth, use
{depth: null}
option:To anyone using Deno (I didn’t test this anywhere else):
To make
console.dir
disregard depth limits, pass a second argument with depth set toInfinity
:console.dir(body, { depth: Infinity });
Based on @A1exandr Belan’s answer, but changing
null
toInfinity
, since it seemsconsole.dir
takesnull
as0
and outputs just a simple[Array]
.