4

I'm fiddling around with a library called bcoin for node. Running the following code:

  chain.on('block', function(block) {
    console.log('Connected block to blockchain:');
    block.txs.forEach(function(t) {
      t.inputs.forEach(function(i) {
        console.log(typeof i, i);
        console.log(JSON.stringify(i));
      });
    });
  });

This is the response I'm getting:

Connected block to blockchain:
object { type: 'coinbase',
  subtype: null,
  address: null,
  script: <Script: 486604799 676>,
  witness: <Witness: >,
  redeem: null,
  sequence: 4294967295,
  prevout: <Outpoint: 0000000000000000000000000000000000000000000000000000000000000000/4294967295>,
  coin: null }
{"prevout":{"hash":"0000000000000000000000000000000000000000000000000000000000000000","index":4294967295},"script":"04ffff001d02a402","witness":"00","sequence":4294967295,"address":null}

Notice that even though the attribute type for example, is shown when we print i, that attribute does not exist when we JSON.stringify the object. If I tried to console.log(i.type) I'd get undefined.

How is that possible? And what is a good way of debugging what's going on with an object?

3
  • You your inner most function add: if ( i ) {, that will take care of i being null or undefined. Commented Oct 24, 2017 at 6:49
  • I'm trying to understand why it's undefined in the first place. Commented Oct 24, 2017 at 6:51
  • then expand your logic and be more verbose, test 't' before looping Commented Oct 24, 2017 at 6:52

1 Answer 1

1

JSON.stringify will only includes enumerable properties that are not functions.

So if you define a property and set as non-enumerable, it will not be a part of JSON string.

var obj = {
  a: 'test'
};

// Non-enumerable property
Object.defineProperty(obj, 'type', {
  enumerable: false,
  value: 'Test'
});

// Get property
Object.defineProperty(obj, 'type2', {
  get: function(){
    return 'Test 2'
  }
});


console.log(JSON.stringify(obj), obj);
console.log(obj.type, obj.type2)

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting. Why are you able to access obj.type while I cannot access i.type?
This is a node application, so it's not running inside the browser. Regarding the object not being the same, the code above is what I'm running, not sure how it is possible for it to be mutated in the way I'm running it.
Although this is interesting, it doesn't answer the question.
@OrWeinberger Thanks for responding. Also, as I previously stated, type is not the only property missing. My suggestion, you should check if any you are overriding an internal function. This might be done my some library. This is because, <Script: 486604799 676> is not a valid value in JS Object. So there is some mechanism that is either printing the values in special way or removing values in stringify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.