【问题标题】:Parsing array of data and sending with nodemailer?解析数据数组并使用nodemailer发送?
【发布时间】:2017-04-18 19:40:24
【问题描述】:

我有一组数据,我想使用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 中创建一个循环,然后循环遍历数据。我确实尝试了以下方法,但似乎我能做的事情有限制。

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

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

【问题讨论】:

    标签: javascript arrays node.js nodemailer


    【解决方案1】:

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

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

    否则,您可以使用下面的数组函数进行解析,其中我使用了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
    

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

    【讨论】:

    • 啊当然,我没有使用模板!这很好用。谢谢!
    • 谢谢,很有帮助
    • 函数中的'a'参数是什么?
    • 第一次迭代时,a 是在reduce 函数的第二个参数中作为空字符串传递的种子值。在随后的迭代中,a 是之前的迭代结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多