How to convert json stored in DB into csv using meteor -


i want download csv file generated database (nodedb) has following entries.
these entries should act headers only.

{          "meta": {          "template_name": "b",           "template_group": "product",          "keywords": [             "cc"         ],           "template_subgroup": ""     },      "varients": [         {             "name": "brand",              "datatype": "text",         }      ] } 

i have written following code:

html:

<template name="templateforcsv">     <a href="{{pathfor 'csv'}}" target="_blank">download csv</a> </template> 

js:

if (meteor.isserver) {   meteor.startup(function () {     //csv download     var datacursor=nodedb.find({});     if (datacursor.count() === 0) {       for(var i=1; i<=datacursor.length; i++) {         //example now, has changed         nodedb.insert({brand: "brand" + i,price: "price" + i, description:"description" + i});       }     }   });  }  router.route('/csv', {   where: 'server',   action: function () {     var filename = 'data.csv';     var filedata = "";      var headers = {       'content-type': 'text/csv',       'content-disposition': "attachment; filename=" + filename     };     var records = nodedb.find();     // main problem. build csv string. oversimplified. you'd have escape quotes , commas.     records.foreach(function(rec) {       filedata += rec.meta + "," + rec.varients + "," +  "\r\n";     });     this.response.writehead(200, headers);     return this.response.end(filedata);   } }); 

the csv file downloaded blank. what's happening?

first, assume nodedb works fine. problem route not defined /csv, try below test.

<template name="templateforcsv">     <a href="/csv" target="_blank">download csv</a> </template> 

if still turns blank, guess nodedb blank, try below test.

router.route('/csv', function () {     var filename = 'data.csv';     var filedata = "hello, world";      var headers = {       'content-type': 'text/csv',       'content-disposition': "attachment; filename=" + filename     };      this.response.writehead(200, headers);     this.response.end(filedata);   }, {where: "server"}); 

the csv file "hello, world" should downloaded. note changed syntax bit on above use.

try , let know if works.


Comments