Search code examples
javascriptnode.jsvariable-assignmenttyped-arrays

Cannot assign values to typed array buffer


How come the value is not getting assigned as expected? No errors are thrown.

fs = require('fs');
var path="data.dat";
var f = fs.createReadStream(path,{highWaterMark:1024*1024*128})
var stat = fs.statSync(path);
var stream = fs.createReadStream(path,{start:0,end:stat.size-1});
var dat = new Float64Array(stat.size);
var idx = 0;
stream.on('data',(chunk)=>{
    var temp = new Float64Array((new Uint8Array(chunk)).buffer);
    for(var i = 0; i< temp.length;++i){
        dat[idx++]=temp[i];
        console.log(temp[i]); //this will log the expected values
        console.log(dat[idx]); //this will log a bunch of 0
    }

});
stream.on('end',function(){
    //do_stuff(dat);
});

Solution

  • Urrgg... I feel stupid for asking. The answer is console.log(dat[idx-1]);