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: http://jsfiddle.net/uselesscode/qm5ag/

So I tried putting my button into a template, with “Records” being the name of a collection:

Template.makefile.events({
 	 'click button': function(event){
	 Records.forEach(function(rec) {
            FileVar += rec.name + "," + 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?

e.g.

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

1 Like

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…

like…

result.forEach(function(){

})

or

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

_.each(result,function(document){
// document._id or document.name
})

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

1 Like

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

1 Like

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

2 Likes