parsing数组数组并使用nodemailer发送数据?

我想要使​​用NodeMailer在表中发送一组数据,如下所示:

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

我正在试图做的,我在我的HTML中创build一个循环,然后通过数据循环。 我确实尝试了下面的内容,但似乎有什么限制我可以做的。

  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'); } }) 

任何人都可以指出我要去哪里错了,我需要做些什么来纠正我的问题。

看起来你已经尝试使用results.forEach((item)但是你把它放在引号'result.forEach((item)'这是一个string,根本不会执行。

你可能已经在你的页面中使用了这种语法,当你使用视图引擎jadeswig等将为你parsing。 但是在这里,你应该手动调用它们来parsing这种语法。

否则,你可以用下面的数组函数进行parsing,我已经使用了array.reduce ,这很方便,并且会很好的parsing。

您可以尝试相同的方式来生成content ,并将其附加到HTML如下。

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