node.js - How to export csv nodejs -


hey trying export csv node.js (pulling data mongodb). have data being pulled , separated commas , all, trying figure out how send it... sticking code in routes file. advice on how take array of data , send user straight download on request.

here code: (i attempted the bottom part of code second function)

exports.downloadcontacts = function(req, res) {     async.waterfall([         function(callback) {             var source = [];             friend.find({userid: req.signedcookies.userid}, function(err, friends) {                 if(err) {console.log('err friends download');                 } else {                     var usermap = {};                     var friendids = friends.map(function (user) {                         usermap[user.friend_id] = user;                         return user.friend_id;                     });                     console.log(friends);                     user.find({_id: {$in: friendids}}, function(err, users) {                         if(err) {console.log(err);                          } else {                             for(var = 0; < users.length; i++) {                                 console.log('users')                                 //console.log(users[i]);                                 source.push(users[i].firstnametrue, users[i].lastnametrue, users[i].emailtrue, users[i].phone, users[i].emaillist, users[i].phonelist)                             }                             console.log(source);                             callback(null, source);                         }                       });                 }               });          }     ],     function(err, source) {         var result = [];          res.contenttype('csv');          csv()         .from(source)         .on('data', function(data){              result.push(data.join());         })         .on('end', function(){             res.send(result.join('\n'));         });     });      }; 

have tried content type "application/octet-stream"

res.set('content-type', 'application/octet-stream'); res.send(<your data>); 

or simply

res.send(new buffer(<your data>)); 

express send() docs.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)