node.js - nodejs throws offset out of bound error on file read using fstat -


hi using nodejs , scoket io pushing data target. reading json data json file push target.

am using fstat of fs module read file whenever there concurren correction or after time throws offset out of bound error.

here code snippet.

   fs.stat(filename, function(error, stats) {       fs.open(filename, "r", function(error, fd2) {          var buffer = new buffer(stats.size);            fs.read(fd2, buffer, 0, buffer.length, null, function(error, bytesread, buffer) {             var data = buffer.tostring("utf8", 0, buffer.length);             fs.close(fd2);      })     }) 

this error throwing

  fs.js:457   binding.read(fd, buffer, offset, length, position, wrapper);       ^   error: offset out of bounds       @ object.fs.read (fs.js:457:11)       @ /var/custom_nodejs_projects/js/server.js:84:14       @ object.oncomplete (fs.js:107:15) 

can guide me here issue struggling past 2 days.

if file exists empty (0 bytes), calling fs.read() fail error you're showing.

so before reading it, make sure stats.size isn't 0.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -