解析数据数组并使用nodemailer发送?

K20*_*0GH 1 javascript arrays node.js 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' 
   }]
Run Code Online (Sandbox Code Playgroud)

我想要做的就是在 HTML 中创建一个循环,然后循环访问数据。我确实尝试了以下方法,但似乎我能做的事情受到限制。

  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');
    }
  })
Run Code Online (Sandbox Code Playgroud)

谁能指出我哪里出了问题以及我需要做什么来纠正我的问题。

Aru*_*una 5

看来您已尝试使用,results.forEach((item)但您将其放在引号内'result.forEach((item)',这是一个字符串,根本不会执行。

当您使用诸如 等的视图引擎时,您可能已经在页面中使用了这种语法,jade它们swig将为您进行解析。但在这里,您应该手动调用它们来解析这种语法。

否则,您可以使用数组函数进行解析,如下所示,我使用过array.reduce该函数,这很方便并且可以很好地进行解析。

您可以尝试同样的方法来生成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
Run Code Online (Sandbox Code Playgroud)

    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
Run Code Online (Sandbox Code Playgroud)