1

I'm creating a meteor app that has a user login system and a button. A user can press the button to add their username to a collection, until it is removed by a super-user.

I want to take note each time a user clicks the button, and download that information in a .csv file or a similar format. So, for example, say user A presses the button at time t1, user B presses it at time t2, user C presses it at t3, and then user A presses it again at time t4. I want to export a record of these interactions in a nice format. ***Note: the button clicks aren't stored in an array so the solution to this question (How to export JavaScript array info to csv (on client side)?) doesn't help me -- unless I'm misunderstanding something, which is totally possible!

I started working through this tutorial for exporting data from meteor apps (https://themeteorchef.com/tutorials/exporting-data-from-your-meteor-application) but it quickly became complicated, and I'm not sure how to keep track of the button clicks using this method.

This seems like a relatively simple task. Is there an easy work-around or common method that I'm missing out on? Thanks!

C Pritt
  • 13
  • 3
  • Possible duplicate of [How to export JavaScript array info to csv (on client side)?](https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side) – Avinash Roy Jun 28 '17 at 12:29
  • Who is to receive the exported file and how are they to receive it? Directly from the browser? (in which case it is always the current user) Or via email (in which case it might be some specific user) – Michel Floyd Jun 28 '17 at 22:54
  • Only myself and/or one specific admin account should be able to receive the file, so it seems as if I'll want to do this via email. (I really just need the data for my own research, so it doesn't matter if it's downloadable from the browser or not) – C Pritt Jun 28 '17 at 23:03
  • If there are frequent changes you might want to just do this on demand from the browser but restrict which user(s) can do it. The solutions for emailing a file and a direct download will be slightly different. – Michel Floyd Jun 29 '17 at 07:14
  • This is two tasks: 1) Storing the button click counts and 2) exporting the data to a csv. Take advantage of the built in mongodb integration. Store button click counts in either the user's document or some arbitrary collection. You could then set up the csv generation on a schedule or on demand. Generation of csv from json is pretty straight forward. If you need more help let me know and I can write up a how to. – Jeremiah Jun 29 '17 at 19:24
  • @Jeremiah thanks for the tip! I think I can figure it out from here so I'll give it a shot, and let you know if I get desperately stuck again. Thanks so much for your help! – C Pritt Jun 30 '17 at 16:57
  • Still running into problems... I have all the clicks stored in a collection. To download, I have tried using mongoexport with no success and also ran into errors with papa parse. Many apologies for being clueless and I would definitely appreciate any help you can give! @Jeremiah – C Pritt Jun 30 '17 at 22:43

1 Answers1

1

Sorry for the delay, the holiday kept me away.

Since you're using Meteor, you have access to a great Mongo API (docs) that allows you to query and transform the data in whatever way you'd like. So simply query whatever you defined your Mongo collection as in Meteor (I'll use ButtonClicks as an example)

let buttonClicks = ButtonClicks.find({}).fetch(); // Finds all button clicks

Then transform the data and use the Node fs (docs) package to write the transformed data to a .csv file.

buttonClicks = buttonClicks.map((click) => {
    // transform your json to csv format here.
    // I encourage you to write this yourself ;).
});

fs.writeFile('buttonClicks.csv', buttonClicks, (err) => {
    if (err) throw err;
    console.log('The file has been saved!');
});
Jeremiah
  • 813
  • 7
  • 16