Javascript 解析数据数组并使用NodeEmailer发送?
我有一个数据数组,我想用nodeEmailer发送到一个表中,它看起来像:Javascript 解析数据数组并使用NodeEmailer发送?,javascript,arrays,node.js,nodemailer,Javascript,Arrays,Node.js,Nodemailer,我有一个数据数组,我想用nodeEmailer发送到一个表中,它看起来像: 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' },
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.forEachitem,但你把它放在引号“result.forEachitem”中,这是一个字符串,根本不会执行 当您使用jade、swig等视图引擎时,您可能已经在页面中使用了这种语法,这些引擎将为您进行解析。但是在这里,您应该手动调用它们来解析这种语法 否则,您可以使用array函数进行解析,如下所示,我使用了array.reduce,它非常方便,可以很好地进行解析 您可以尝试同样的方法来生成内容并将其附加到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结果=[{
asin:'B01571L1Z4',
url:'domain.com',
宠儿:错,
创建日期:“2016-11-18T19:08:41.662Z”,
更新日期:“2016-11-18T19:08:41.662Z”,
id:'582f51b94581a7f21a884f40'
},
{
asin:'B01IM0K0R2',
url:'domain2.com',
宠儿:错,
创建日期:“2016-11-16T17:56:21.696Z”,
更新日期:“2016-11-16T17:56:21.696Z”,
id:'B01IM0K0R2'
}];
var内容=结果。还原函数A,b{
返回a++b.asin++b.url++b.favorite++b.reatedAt+;
}, ;
console.logcontent;
/*
var sendUpdateMerch=transporter.templateSender{
发件人:'测试',//发件人地址
主题:“测试更新”,//主题行
html:'ASINUrlFavouritecreatedAt'+content+//html正文
};
SendUpdateMerch{
致:'domain@gmail.com'
},{results},函数错误,信息{
伊弗{
console.logerr;
}否则{
console.log'Done';
}
}
*/ 似乎您试图使用results.forEachitem,但您将其放在引号“result.forEachitem”中,该引号是一个字符串,根本不会执行 当您使用jade、swig等视图引擎时,您可能已经在页面中使用了这种语法,这些引擎将为您进行解析。但是在这里,您应该手动调用它们来解析这种语法 否则,您可以使用array函数进行解析,如下所示,我使用了array.reduce,它非常方便,可以很好地进行解析 您可以尝试同样的方法来生成内容并将其附加到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结果=[{
asin:'B01571L1Z4',
url:'domain.com',
宠儿:错,
创建日期:“2016-11-18T19:08:41.662Z”,
更新日期:“2016-11-18T19:08:41.662Z”,
id:'582f51b94581a7f21a884f40'
},
{
asin:'B01IM0K0R2',
url:'domain2.com',
宠儿:错,
创建日期:“2016-11-16T17:56:21.696Z”,
更新日期:“2016-11-16T17:56:21.696Z”,
id:'B01IM0K0R2'
}];
var内容=结果。还原函数A,b{
返回a++b.asin++b.url++b.favorite++b.reatedAt+;
}, ;
console.logcontent;
/*
var sendUpdateMerch=transporter.templateSender{
发件人:'测试',//发件人地址
主题:“测试更新”,//主题行
html:'ASINUrlFavouritecreatedAt'+content+//html正文
};
SendUpdateMerch{
致:'domain@gmail.com'
},{results},函数错误,信息{
伊弗{
console.logerr;
}否则{
console.log'Done';
}
}
*/当然,我没有使用模板!这很有效。谢谢谢谢,这非常有用。函数中的“a”参数是什么?在第一次迭代中,a是种子值,在reduce函数的第二个参数中作为空字符串传递。在随后的迭代中,a是上一次迭代的结果。啊,当然,我没有使用模板!这很有效。谢谢谢谢,这非常有用。函数中的“a”参数是什么?在第一次迭代中,a是种子值,在reduce函数的第二个参数中作为空字符串传递。在后续迭代中,a是前一个迭代结果。