Monday, 29 May 2017

How can I see the contents of a JavaScript Map using the node debugger?

I am using the console debugger in NodeJS and want to watch a Map object. Here is the simple test script I want to practice on.

'use strict'

const data = new Map()

const readline = require('readline-sync')
let input
do {
  input = String(readline.question('enter command: ')).trim()
  debugger
  if (input.indexOf('add ') === 0) {
    const space = input.indexOf(' ')
    const item = input.substring(space).trim()
    console.log(`adding '${item}'`)
    let qty = 1
    debugger
    if (data.has(item)) qty = data.get(item) + 1
    data.set(item, qty)
  }
  if (input.indexOf('list') === 0) {
    data.forEach( (val, key) => {
        process.stdout.write(`${key}\t${val}\n`)
    })
  }
} while (input !== 'exit')

So I start the script in debug mode and attach a watcher to the data object.

node debug simpleDebug.js
< Debugger listening on 127.0.0.1:5858
connecting to 127.0.0.1:5858 ... ok
break in simpleDebug.js:2
  1 
> 2 'use strict'
  3 
  4 const data = new Map()
debug> watch('data')
debug> c
debug> enter command: add cheese
break in simpleDebug.js:10
Watchers:
  0: data = {"handle":15,"type":"map","text":"#<Map>"}

  8 do {
  9     input = String(readline.question('enter command: ')).trim()
>10     debugger
 11     if (input.indexOf('add ') === 0) {
 12         const space = input.indexOf(' ')
debug>

As you can see, the debugger does not show the values stored in the map. I assume these are part of the "text" key but how can I add a watcher to that?



via Mark Tyers

No comments:

Post a Comment