Skip to content Skip to sidebar Skip to footer

Parsing Array Of Data And Sending With Nodemailer?

I've got an array of data that I want to send in a table using NodeMailer that looks something like: var results = [ { asin: 'B01571L1Z4', url: 'domain.com', favourite

Solution 1:

It seems you have tried to use results.forEach((item) but you placed this inside the quotes 'result.forEach((item)' which is a string and won't execute at all.

You may have used this kind of syntax in your page when you used the view engines like jade, swig etc which will do the parsing for you. But here, you should call them manually to parse this kind of syntax.

Otherwise, you can do the parsing with the array function as below where I have used array.reduce which is handy and will do the parsing nicely.

You can try the same to generate the content and append it to the html as below.

    html: '<div><table><thead><tr><th>ASIN</th><th>Url</th><th>Favourite</th><th>createdAt</th></tr></thead><tbody>' + 
content + '</tbody></table></div>' // html body

var results = [ { 
    asin: 'B01571L1Z4',
    url: 'domain.com',
    favourite: false,
    createdAt: '2016-11-18T19:08:41.662Z',
    updatedAt: '2016-11-18T19:08:41.662Z',
    id: '582f51b94581a7f21a884f40' 
  },
  { 
    asin: 'B01IM0K0R2',
    url: 'domain2.com',
    favourite: false,
    createdAt: '2016-11-16T17:56:21.696Z',
    updatedAt: '2016-11-16T17:56:21.696Z',
    id: 'B01IM0K0R2' 
   }];

var content = results.reduce(function(a, b) {
  return a + '<tr><td>' + b.asin + '</a></td><td>' + b.url + '</td><td>' + b.favourite + '</td><td>' + b.reatedAt + '</td></tr>';
}, '');

console.log(content);

/*
var sendUpdatedMerch = transporter.templateSender({
      from: '"Test" <domain1@gmail.com>', // sender address
      subject: 'Test Updates', // Subject line
      html: '<div><table><thead><tr><th>ASIN</th><th>Url</th><th>Favourite</th><th>createdAt</th></tr></thead><tbody>' + content + '</tbody></table></div>' // html body
  });


  sendUpdatedMerch({
    to: 'domain@gmail.com'
  }, {results}, function(err, info){
    if(err){
      console.log(err);
    } else {
      console.log('Done');
    }
  })
  
  */

Post a Comment for "Parsing Array Of Data And Sending With Nodemailer?"