解析数据数组并使用 nodemailer 发送?
Parsing array of data and sending with nodemailer?
我有一组数据,我想使用 NodeMailer
在 table 中发送,看起来像:
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');
}
})
任何人都可以指出我哪里出错了以及我需要做些什么来纠正我的问题。
您似乎尝试过使用 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
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
在 table 中发送,看起来像:
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');
}
})
任何人都可以指出我哪里出错了以及我需要做些什么来纠正我的问题。
您似乎尝试过使用 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
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');
}
})
*/