2016-12-04 14 views
1

は、私のようなものに見える私は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' 
    }] 

私は私が私のHTML内部ループと、ループスルーを作成するためにやろうとしていますデータ。私は以下を試しましたが、私ができることには限界があるようです。

var sendUpdatedMerch = transporter.templateSender({ 
     from: '"Test" <[email protected]>', // 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: '[email protected]' 
    }, {results}, function(err, info){ 
    if(err){ 
     console.log(err); 
    } else { 
     console.log('Done'); 
    } 
    }) 

私が間違っている場所を指摘してもらい、私の問題を解決するために何をする必要があるのでしょうか。

答えて

1

results.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" <[email protected]>', // 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: '[email protected]' 
 
    }, {results}, function(err, info){ 
 
    if(err){ 
 
     console.log(err); 
 
    } else { 
 
     console.log('Done'); 
 
    } 
 
    }) 
 
    
 
    */

+0

、私はテンプレートを使用していませんよ!それは素晴らしい作品です。ありがとう! – K20GH

+0

@ K20GH素晴らしい:-) – Aruna

関連する問題