0

Ich habe Schwierigkeiten beim Export einer Tabelle nach csv in meteor/blaze. Ich folge: [http://rafaelquintanilha.com/export-your-json-data-to-csv-format/][1].Export Tabellenwerte Meteor Blaze

Ich habe eine Template.event, die den Export Button

Template.export.onCreated(() => { 
    Template.instance().subscribe('table'); 
}); 
Template.export.helpers({ 
    exportContacts() { 
    return Contacts.find(); 
    } 
}); 
Template.export.events({ 
    'click .export-data'() { 
    MyAppExporter.exportAllContacts(); 
    } 
}); 

es ruft exportAllContacts() in einem globalen Helfer ruft

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

und der Helfer ein Meteor.method exportContacts auslöst

Meteor.methods({ 
    exportContacts: function() { 
     let fields = [ 
      "Email", 
      “Some Contact", 
      "Created Date", 
      "Hard Bounce", 
      "Unsubscribed" 
     ]; 
     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); 
      data.push([ 
       c.contact.emailAddress, 
       c.contact.someContact, 
       c.contact.creationDate, 
       c.contact.hardBounceBack, 
       c.contact.unsubscribed 
      ]); 
     console.log("DATA", data) 
     return {fields: fields, data: data}; 
     }); 
    } 
    } 
}); 

ich halte einen Fehler bekommen, dass „nicht emailaddress ex definiert ist portContacts.js: 20: 17

20160426-22:00:47.957(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"[email protected]","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) 

aber ich kann nicht scheinen, herauszufinden, wie Sie auf die Kontakte zugreifen. Ich logge mich aus (siehe oben in Logs). Jede Hilfe wäre willkommen.

ADDED LOGS Lassen Sie Kontakte = Contacts.find(). Fetch(); console.log (Kontakte)

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] ] } ] 

c.contact [0] .emailAddress abgemeldet

I20160427-09:22:08.142(-4)? Inside Loop {"_id":"dRnXRdZrbR9CYdmBx","contact":[{"emailAddress":"[email protected]","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) 
+0

Können Sie den Link [Bearbeiten] zu Ihrer Frage verwenden, um einige Beispieldokumente aus dem Ergebnis, das Sie erhalten, mit 'let contacts = Contacts.find(). Fetch(); console.log (Kontakte) '? Von der bloßen Untersuchung ist 'c.contact' ein Array und Sie versuchen, auf eine Eigenschaft von einem Array daher den Fehler zuzugreifen. Können Sie auch 'c.contact [0] .emailAddress' versuchen? – chridam

+0

Ich habe die Protokolle für Sie chridam hinzugefügt –

Antwort

0

Innerhalb der _each Schleife greifen Sie auf den Wron zu g Datenelemente. Sie können auch eine _.each-Schleife anstelle der äußeren for-Schleife verwenden. Wenn Sie dies tun:

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

Dies sollte Ihr Problem lösen. Auf diese Weise durchlaufen Sie die äußeren Kontakte, die vom Abruf zurückkommen, und durchlaufen dann das Kontakt-Array von jedem Element. Dies sollte der direkteste Weg sein, um zu den Daten zu gelangen, nach denen Sie suchen.

0

Ihr Problem ist diese Linie: _.each(contactString, function(c) {

Es sollte lesen: _.each(contact, function(c) {

:)

Verwandte Themen