Home > Back-end >  NodeJS not showing full object, only [Object], for response
NodeJS not showing full object, only [Object], for response

Time:04-04

When sending all documents from the MondoDB database I'm getting the response

data: { list: [ [Object], [Object] ] }

This is the correct number of documents, and they are objects, but I want to know how I can display the entire document, including nested objects, not just the [Object]. How is this possible? Here's my code:

Doc.find({})
            .then((list) => {
                let listToSend = [];

                // push the response data to the array
                for (let i = 0; i < list.length; i  ) {
                    listToSend.push(list[i]);
                }

                // send the list once all documents are pushed
                res.send(({ data: listToSend }));
            })

CodePudding user response:

You can use JSON.stringify, and get some nice formatting

console.log(JSON.stringify(myObject, null, 4));

console.log(JSON.parse(JSON.stringify(myObject)));

CodePudding user response:

If you want to increase the depth of the inspection output (the default is 2) you can manually call the inspect function (here I used 6 as example):

import { inspect } from 'util'

console.log(inspect(myObject, { depth: 6 }))

Or if you want that to happen all the time by default, you can set the defaultOptions:

import { inspect } from 'util'

inspect.defaultOptions.depth = 6
  • Related