javascript - Why is fs.readFile returning a buffer? -
I have already mentioned that is it, I do not believe that my problem is in the misunderstanding of Asyn.
Here is the relevant part of my module
var fs = is required ('FS'); Var q = requirement ('q'); Var u = is required ('../ utils / json'); Var indexFile = './data/index.json'; Function getIndex () {var def = q.defer (), promise = def.promise, obj; Fs.readFile (indexFile, function (error, data) {if (mistake) {throw error; def.reject (err);} console.log ('data', data); def.resolve (obj);}); Return promise; } When I log out 'data', I am getting buffer (bottom) instead of JSON content of that file. & lt; Buffer 5b7b 22 68 65 6c6c6f223a22 77 6f72 6c 64 22 7d5d> gt; Any thoughts on why? According to
, if the encoding option is not passed, Read function will return a buffer If you pass a value for encoding, then it will return a string with that encoding:
fs .readFile ('/ etc / passwd', 'utf-8', callback)
Comments
Post a Comment