Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writes [object Object] to csv file? #101

Closed
ganna-shmatova opened this issue May 25, 2015 · 3 comments
Closed

Writes [object Object] to csv file? #101

ganna-shmatova opened this issue May 25, 2015 · 3 comments

Comments

@ganna-shmatova
Copy link

According to docs I can write arrays of objects. However the resulting .csv file has '[object Object]' field after field, instead of the object's fields and then rows of the objects' values.

function makeReport(stamp, cb){
    var outputDir = getDir(stamp);
    var file = path.join(outputDir, 'shortcode_report.csv');
    var report = [];
    for(var k in shortcodeStats)
        report.push(shortcodeStats[k]);
    writeReport(file, report, cb);
}

function writeReport(file, data, cb){
    var existsAlready = fs.existsSync(file);
    var csvStream = csv.createWriteStream({
        headers: !existsAlready,
        includeEndRowDelimiter: true,
    });
    csvStream.on('finish', function(err){
        cb(err, file);
    });
    csvStream.pipe(fs.createWriteStream(file, {flags:'a'}));
    csvStream.write(data);
    csvStream.end();
}

stats object looks like this:

stats = {
    'shortcode': 'beftfit_325454',
    'boolean req failed occurance': 0,
    'eligibly failed occurance': 0,
    'boolean hois': '',
    'eligiblity hois': ''
};

output was:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
@dustinsmith1024
Copy link
Contributor

Not totally sure. Most the examples have .pipe coming after .write.

There are tests for this so it should work in node 4 and 5 at least. https://github.com/C2FO/fast-csv/blob/master/test/formatting.test.js#L24

var ws = fs.createWriteStream("my.csv");
csv
   .write([
       {a: "a1", b: "b1"},
       {a: "a2", b: "b2"}
   ], {
        headers: true
        transform: function(row){
            return {
                A: row.a,
                B: row.b
            };
        }
   })
   .pipe(ws);

@bllevy
Copy link

bllevy commented Jun 19, 2017

I'm running into this same problem working in node v6.1.0.

@doug-martin
Copy link
Contributor

I think the two different write methods are being conflated. The write method in the inital example is the the write method that is provided by the node streams api, and should be called one row at a time.

The write report should be changed to.

function writeReport(file, data, cb){
    var existsAlready = fs.existsSync(file);
    var ws = fs.createWriteStream(file, {flags:'a'});
    var csvStream = csv.write(data, {
        headers: !existsAlready,
        includeEndRowDelimiter: true,
    });
    csvStream
        .on('finish', function(err){
            cb(err, file);
        })
        .pipe(fs.createWriteStream(file, {flags:'a'}));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants