Newbie question: how can I put data from a collection into a variable, rather than displaying it?


I’d like to be able to let users download a plain text file of certain data from a collection, but can’t figure out how to get that data into a variable.

I had intended to use put data into a text blob for downloading like this, which is something I’ve done in non-Meteor projects, like this:

So I tried putting my button into a template, with “Records” being the name of a collection:{
 	 'click button': function(event){
	 Records.forEach(function(rec) {
            FileVar += + "," + rec.address + "<br>"; }); 


but this doesn’t work; console.log says “forEach is not a function” when I do it this way. Is an event the wrong place to do this? I’m probably missing something pretty basic here, but would appreciate advice.


so where is the Records from? i dont see on your snippet where the collection was fetched?


Records = records.find().fech();


records I just have in the .js file, neither in Meteor.isClient or Meteor.isServer, as

Records = new Meteor.Collection(“records”);

I intend to eventually narrow that down, but I still thought forEach would grab the entire collection in this case.


oh ok…

Records = new Meteor.Collection(“records”); is just saying that “Records” is a collection variable…

thus you can use it as…

var result = Records.find() this will make the result variable a collection cursor…

so the for each will be placed on the “result” variable that will hold the fetched documents from Records Collection…





let say your records collection has a field _id and name below is how you can access them…

// document._id or

sorry for my english im not very good if you are having a hard time understanding me…


Oh, thank you! I didn’t realize I needed the extra line, but now it’s working as expected. I appreciate it!


wow :blush:finally i helped someone! :smile: thank you too! :slight_smile: have a nice day!!