Posted By

canuto on 01/09/19


Tagged

csv excel export restdb codehook


Versions (?)

restdb.io - code hook to export to CSV / Excel


 / Published in: JavaScript
 

This snippet was created using restdb.io Custom REST routes (see docs here). It will export the data of the "players" collection to CSV/Excel. The content type of the Page is "text/javascript".

  1. // #!/javascript
  2.  
  3. const onGET = async (req, res) => {
  4. // set up query
  5. const query = {};
  6. if (req.params.q) {
  7. query = JSON.parse(req.params.q);
  8. }
  9.  
  10. // header row of CSV output
  11. const output = ["Name, Score, Email, Category"];
  12.  
  13. // run query
  14. const data = await db.get('/rest/players', query, {"$orderby": {"first-name": 1}});
  15.  
  16. // add rows to output
  17. data.forEach((x) => {
  18. output.push(`${x['name']}, ${x['score']}, ${x['email']}, ${x['category']}`);
  19. });
  20.  
  21. // return output as text
  22. res.end({"text": output.join("\n")});
  23. }

Report this snippet  

You need to login to post a comment.