Brandon Baum Brandon Baum - 2 years ago 123
Javascript Question

Export table values Meteor Blaze

I am running into some difficulty exporting a table to csv in meteor/blaze. I am following: [][1].

I have a Template.event that is triggering the export button

Template.export.onCreated( () => {
exportContacts() {
return Contacts.find();
'click .export-data' () {

it is calling exportAllContacts() in a global helper

MyAppExporter = {
exportAllContacts: function() {
var self = this;"exportContacts", function(error, data) {
if ( error ) {
return false;
var csv = Papa.unparse(data);
_downloadCSV: function(csv) {
var blob = new Blob([csv]);
var a = window.document.createElement("a");
a.href = window.URL.createObjectURL(blob, {type: "text/plain"}); = "contacts.csv";

and the helper is calling a Meteor.method exportContacts

exportContacts: function() {
let fields = [
“Some Contact",
"Created Date",
"Hard Bounce",
let data = [];
let contacts = Contacts.find().fetch();
for(let i = 0; i < contacts.length; i++) {
let contact = contacts[i];
let contactString = JSON.stringify(contact);
_.each(contactString, function(c) {
console.log("Inside Loop", contactString);
console.log("DATA", data)
return {fields: fields, data: data};

I keep getting an error that “emailAddress is not defined exportContacts.js:20:17

20160426-22:00:47.957(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"","someContact":"No","creationDate":"N/A","hardBounceBack":"N/A","unsubscribed":"N/A"}]}

I20160426-22:00:48.029(-4)? Exception while invoking method 'exportContacts' ReferenceError: emailAddress is not defined
I20160426-22:00:48.029(-4)? at server/methods/exportContacts.js:20:17
I20160426-22:00:48.029(-4)? at Function._.each._.forEach (packages/underscore.js:142:22)
I20160426-22:00:48.029(-4)? at _loop (server/methods/exportContacts.js:17:7)

but I cannot seem to figure out how to access the contacts. I am logging it out (see above in logs). Any help would be appreciated.

let contacts = Contacts.find().fetch(); console.log(contacts)

I20160427-09:06:23.484(-4)? CONTACTS [ { _id: 'dRnXRdZrbR9CYdmBx', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'LHmW4R9PLM5D7cZxr', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'jBdqQXz2b8itXJowX', contact: [ [Object] ] },
I20160427-09:06:23.484(-4)? { _id: 'bnDvNGX3i879z4wr2', contact: [ [Object] ] } ][0].emailAddress logged out

I20160427-09:22:08.142(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"","someContact":"No","creationDate":"N/A","hardBounceBack":"N/A","unsubscribed":"N/A"}]}
I20160427-09:22:08.217(-4)? Exception while invoking method 'exportContacts' TypeError: Cannot read property '0' of undefined
I20160427-09:22:08.217(-4)? at server/methods/exportContacts.js:21:7
I20160427-09:22:08.217(-4)? at Function._.each._.forEach (packages/underscore.js:142:22)
I20160427-09:22:08.217(-4)? at _loop (server/methods/exportContacts.js:18:7)
I20160427-09:22:08.218(-4)? at [object Object].exportContacts (server/methods/exportContacts.js:15:46)

Answer Source

Within the _.each loop you are accessing the wrong data items. You can also use a _.each loop instead of the outer for loop too. If you do :

let contacts = Contacts.find().fetch();
_.each(contacts, function(contact) {
  _each(, function(c) {
              "email": c.emailAddress,
              "contact": c. someContact,
              "creationDate" : c.creationDate,
              "bounceBack": c.hardBounceBack,
              "unsubscribed": c.unsubscribed

This should solve your problem. This way you are looping through the outer contacts that is coming back from the fetch and then looping through the contact array from each element. This should be the most direct way to get down to the data you are looking for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download