Parsing array of data and sending with nodemailer?

浪子不回头ぞ 提交于 2021-01-06 10:52:25

问题


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

What I am trying to do i to create a loop inside my HTML and then loop through the data. I did try the below, but it seems there are limitations on what I can do.

  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>{{result.forEach((item) => {<tr><td>{{asin}}</a></td><td>{{url}</td><td>{{favourite}}</td><td>{{createdAt}}</td></tr>})}}</tbody></table></div>' // html body
  });

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

Could anyone point out where I am going wrong please and what I need to do to correct my problems.


回答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');
    }
  })
  
  */


来源:https://stackoverflow.com/questions/40956866/parsing-array-of-data-and-sending-with-nodemailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!