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

Writing object ends up with function definitions #158

Closed
ajfisher opened this issue Sep 26, 2016 · 3 comments
Closed

Writing object ends up with function definitions #158

ajfisher opened this issue Sep 26, 2016 · 3 comments

Comments

@ajfisher
Copy link

I'm trying to use the writer to output a CSV file. I have a list of objects that I'm processing with another part of my application and periodically I want to write the data out from those objects as they complete.

At the moment i'm simply doing a csvwriter.write(object) having already wired up the stream. When I do this, I get all the fields on the object correctly, however I then get all the methods that have been attached to the object prototype as well. A simplistic example of this is below:

const fastcsv = require('fast-csv');
const fs = require("fs");

function Place() {
    this.name = Math.random().toString(36).substring(10);
    this.id = Math.round(Math.random() * 100000);
    this.calculated_value = 0;
}

Place.prototype.calculate_something = function () {
    this.calculated_value = this.id * 2;
}

var csvwriter = fastcsv.createWriteStream({ headers: true });
csvwriter.pipe(process.stdout);

for (let i=0; i< 10; i++) {
    let temp_place = new Place();
    console.log(temp_place);
    csvwriter.write(temp_place);
}

If you run this you'll get something approximating the following output:

Place { name: 'c3byb30vrk162yb9', id: 5775, calculated_value: 0 }
name,id,calculated_value,calculate_something
c3byb30vrk162yb9,5775,0,"function () {

    this.calculated_value = this.id * 2;
}"Place { name: '6ie10d92e8p9fi529', id: 68461, calculated_value: 0 }

6ie10d92e8p9fi529,68461,0,"function () {

    this.calculated_value = this.id * 2;
}"Place { name: '6kfbqxd850l3yuv7vi', id: 34590, calculated_value: 0 }

6kfbqxd850l3yuv7vi,34590,0,"function () {

    this.calculated_value = this.id * 2;
}"
-- SNIP --

Which isn't expected. I've tried tracking this through to the write function but must confess I'm not really clear on what that's actually doing in this case...

Searching the docs, it doesn't appear I can limit the fields that are being passed which would get me sorted - however, it feels very brittle to me to taking the object, dumping just the keys I want and then passing that to the writer as that is a point of maintenance that will need to be kept in sync with original object.

@doug-martin doug-martin mentioned this issue Sep 27, 2016
@doug-martin
Copy link
Contributor

Fixed in pr #159 will merge and publish in the morning.

@ajfisher
Copy link
Author

Excellent - thanks for sorting that out!

On Tue, Sep 27, 2016 at 3:33 PM Doug Martin notifications@github.com
wrote:

Fixed in pr #159 #159 will merge
and publish in the morning.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#158 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAHRo4jEkMZFKeMtWw-kyq82WIiwv8sXks5quKq7gaJpZM4KGPh3
.

@doug-martin
Copy link
Contributor

No Problem! Thanks for submitting the issue.

aheuermann added a commit that referenced this issue Jul 27, 2017
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

2 participants