解析数据数组并使用nodemailer发送? [英] Parsing array of data and sending with nodemailer?

查看:41
本文介绍了解析数据数组并使用nodemailer发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用 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: 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内创建一个循环,然后遍历数据.我确实尝试了以下方法,但是看来我的工作有局限性.

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.

推荐答案

似乎您已尝试使用 results.forEach((item).forEach((item)'是一个字符串,根本不会执行.

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.

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

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.

否则,您可以使用下面的array函数进行解析,其中我使用了 array.reduce ,它很方便并且可以很好地进行解析.

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.

您可以尝试使用相同的方法生成 content 并将其附加到html,如下所示.

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

这篇关于解析数据数组并使用nodemailer发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆