I am struggling to write data to a CSV in Node.js.
i tried this code.
async.auto({
user: function(cb){
User.findOne({'memberid': currMemberId}).exec(cb);
},
billing : function(cb){
Payment.find({'memberid' :currMemberId}).exec(cb);
},
trans: function(cb){
Transaction.find({'id':p.ids}) .exec(cb);
}
},function allDone(err, async_data) {
var data= async_data.trans;
createCSV = function(data){
csv().from(data).to(fs.createWriteStream("c:/temp/sample.csv"));
}
});
but it doesn't seem to work. can someone please help? I want to fetch some data from database and export this data to csv file.database I fetching data similar to sample data fields plus help me. Thanks
Without seeing a full example and knowing what the csv dependency is, its tricky to help. However, if it is this one http://www.adaltas.com/projects/node-csv/ - then according to the docs you have the data structured wrong. it should be like this:
var data= ["name",'accountno','amount','type',['test','12323','200','sal']];
i.e. the headers are in the top level array instead of a nested array.
Just a guess really, but might be worth a go...