最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is fs.readFile returning a buffer? - Stack Overflow

programmeradmin2浏览0评论

I have referred to this question already. That is, I don't believe my problem lies in a misunderstanding of async.

Here is the relevant part of my module.

var fs = require('fs');
var q = require('q');
var u = require('../utils/json');

var indexFile = './data/index.json';

function getIndex() {
    var def = q.defer(),
        promise = def.promise,
        obj;

    fs.readFile(indexFile, function(err,data) {
        if (err) {
            throw err;
            def.reject(err);
        }
        console.log('data', data);

        def.resolve(obj);
    });

    return promise;
}

When I log 'data', I'm getting a buffer (below), rather than the JSON content of that file.

<Buffer 5b 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d 5d>

Any thoughts on why?

I have referred to this question already. That is, I don't believe my problem lies in a misunderstanding of async.

Here is the relevant part of my module.

var fs = require('fs');
var q = require('q');
var u = require('../utils/json');

var indexFile = './data/index.json';

function getIndex() {
    var def = q.defer(),
        promise = def.promise,
        obj;

    fs.readFile(indexFile, function(err,data) {
        if (err) {
            throw err;
            def.reject(err);
        }
        console.log('data', data);

        def.resolve(obj);
    });

    return promise;
}

When I log 'data', I'm getting a buffer (below), rather than the JSON content of that file.

<Buffer 5b 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d 5d>

Any thoughts on why?

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Oct 9, 2014 at 2:06 Bryce JohnsonBryce Johnson 6,9017 gold badges46 silver badges52 bronze badges 3
  • 2 as an aside, don't throw err if you want to reject your deferred with it. – Andbdrew Commented Oct 9, 2014 at 2:19
  • @Andbdrew good point. Thanks for the tip. – Bryce Johnson Commented Oct 9, 2014 at 2:23
  • 2 And the reason the API defaults to a buffer instead of a string is that buffers are the only safe way of reading binary data. But you can convert a buffer to a string even after reading it by either calling .toString() directly of appending forcing it into a string context: '' + buffer – slebetman Commented Oct 9, 2014 at 3:44
Add a comment  | 

3 Answers 3

Reset to default 20

As per the Node.js API docs for 'fs' module, if the encoding option isn't passed, the read functions will return a buffer.

If you pass a value for encoding, it will return a string with that encoding:

fs.readFile('/etc/passwd', 'utf-8', callback)

As stated before, fs module requires the encoding option as second parameter.

Also, if you are sure that your file consists utf-8 string, you can use;

fs.readFile(indexFile, function(err,data) {
    if (err) {
        return def.reject(err);
    }

    console.log('data', data.toString());

    def.resolve(obj);
});

Try this... You need to include encoding

fs.readFile(indexFile,'utf8', function(err,data) {
    if (err) {
        throw err;
    }
    //Do something with data
    console.log(data);
});
发布评论

评论列表(0)

  1. 暂无评论