user2607717
user2607717

Reputation: 53

data.buffer.length returning undefined

I have input Readable stream coming in via an external node library. I am trying to read and get the ArrayBuffer and convert it to something else for my program.

input.on(‘data’, function(data){
console.log(data); //returns <Buffer 73 fe 74 fe 95 fe 95 fe b6 fe b6 fe 00 ff 01 ff 37 ff 36 ff 8f ff 8f ff 10 00 10 00 65 00 66 00 95 00 95 00 79 00 7a 00 1b 00 1a 00 be ff be ff 56 ff ... >

console.log(data.buffer); // returns — ArrayBuffer { byteLength: 300 }

console.log(data.buffer.length); //returns undefined
console.log(data.buffer[0]); //returns undefined
});

If the data.buffer is an ArrayBuffer, why am I not able to access it at all?

Upvotes: 4

Views: 2117

Answers (1)

Stas Parshin
Stas Parshin

Reputation: 1703

It looks like your data variable is a Buffer itself and by getting data.buffer you are getting the underlying ArrayBuffer.

https://nodejs.org/api/buffer.html#buffer_buf_buffer

So you should either do something like:

console.log(data.length);
console.log(data[0]);

Or get the data.buffer but be aware that ArrayBuffers don't have much in common with "normal" JS arrays.

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

http://www.javascripture.com/ArrayBuffer

Upvotes: 2

Related Questions