How to download mongo collections as file using iron-router (and ground-db)?

69 views Asked by At

What I want to do: Specify a route (with iron-router), which starts a file download when accessed. The file contains information from the applications mongo-Databases.

Use case: I'm developing a meteor offline web-application which gets also exported as android app. Within the app the user can save several different data/information. The user should be able to export and import that data from inside the app (For example when he wants to switch device). There might be a solution, which is quite different then my (this) approach, I'm of course also open to that.

Additional information: My mongo-Databases are local (client only) using groundDB. It would be perfect if the solution will also work when the meteor-application is running as android app. The actual format of the file is minor (something like csv / json / ... ).

Current Approach:

// Define route...
Router.route('/download', function(){
/**
 * Combine data from different Mongo
 * Collections in one js variable in JSON format:
 **/
  var dataToDownload = [];
  dataToDownload[0] =        Collection.find().fetch();
  dataToDownload[1] = AnotherCollection.find().fetch();

/** 
 * Convert JSON to string for download (not sure
 * if necessary?!):
 **/
  var data = "text/json;charset=utf-8,"
    + encodeURIComponent(JSON.stringify(dataToDownload));

/**
 * This obviosly doesn't work since it's trying to 
 * render a template which has the name equal to 
 * my JSON string:
 **/
  this.render(data);
});

Basically I'm looking for something to replace this.render(data); with sth. that does (in pseudo code) this.download(data);.

0

There are 0 answers