JS - console.log deep display

谭曦
2023-12-01
const util = require('util')
console.log(('inspect db 1 is=',util.inspect(db, {showHidden: false, depth: null})))

console.log(('inspect db 2 is=',util.inspect(db, false, null, true /* enable colors */)));

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

Example

const clone = require('clone')
const config = require('./config')
const util = require('util')



const db = {}

const defaultData = {
  contacts: [
    {
      id: 'richard',
      name: 'Richard Kalehoff',
      handle: '@richardkalehoff',
      avatarURL: config.origin + '/richard.jpg'
    },
    {
      id: 'karen',
      name: 'Karen Isgrigg',
      handle: '@karen_isgrigg',
      avatarURL: config.origin + '/karen.jpg'
    },
    {
      id: 'tyler',
      name: 'Tyler McGinnis',
      handle: '@tylermcginnis',
      avatarURL: config.origin + '/tyler.jpg'
    }
  ]
}

const get = (token) => {
  let data = db[token]
  console.log('db is=',db)
  console.log(('inspect db 1 is=',util.inspect(db, {showHidden: false, depth: null})))

// alternative shortcut
console.log(('inspect db 2 is=',util.inspect(db, false, null, true /* enable colors */)));

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

  if (data == null) {
    data = db[token] = clone(defaultData)
  }

  return data
}

nakamono@ninja MINGW64 ~/Downloads/ReactNano/reactnd-contacts-server2 (master)
$ npm start

> @ start C:\Users\inakamono\Downloads\ReactNano\reactnd-contacts-server2
> node server.js

Server listening on port 5001, Ctrl+C to stop
Authorization=== uhdk9pee
db is= {}
{}       
{}       
{}       
Authorization=== uhdk9pee
db is= { uhdk9pee: { contacts: [ [Object], [Object], [Object] ] } }
{
  uhdk9pee: {
    contacts: [
      {
        id: 'richard',
        name: 'Richard Kalehoff',
        handle: '@richardkalehoff',
        avatarURL: 'http://localhost:5001/richard.jpg'
      },
      {
        id: 'karen',
        name: 'Karen Isgrigg',
        handle: '@karen_isgrigg',
        avatarURL: 'http://localhost:5001/karen.jpg'
      },
      {
        id: 'tyler',
        name: 'Tyler McGinnis',
        handle: '@tylermcginnis',
        avatarURL: 'http://localhost:5001/tyler.jpg'
      }
    ]
  }
}
{
  uhdk9pee: {
    contacts: [
      {
        id: 'richard',
        name: 'Richard Kalehoff',
        handle: '@richardkalehoff',
        avatarURL: 'http://localhost:5001/richard.jpg'
      },
      {
        id: 'karen',
        name: 'Karen Isgrigg',
        handle: '@karen_isgrigg',
        avatarURL: 'http://localhost:5001/karen.jpg'
      },
      {
        id: 'tyler',
        name: 'Tyler McGinnis',
        handle: '@tylermcginnis',
        avatarURL: 'http://localhost:5001/tyler.jpg'
      }
    ]
  }
}
{
    "uhdk9pee": {
        "contacts": [
            {
                "id": "richard",
                "name": "Richard Kalehoff",
                "handle": "@richardkalehoff",
                "avatarURL": "http://localhost:5001/richard.jpg"
            },
            {
                "id": "karen",
                "name": "Karen Isgrigg",
                "handle": "@karen_isgrigg",
                "avatarURL": "http://localhost:5001/karen.jpg"
            },
            {
                "id": "tyler",
                "name": "Tyler McGinnis",
                "handle": "@tylermcginnis",
                "avatarURL": "http://localhost:5001/tyler.jpg"
            }
        ]
    }
}

 类似资料: