使用lodash ._模板进行电子邮件

我正在尝试使用lodash _.template创build一个带有Node.js的HTML电子邮件模板。 当我使用下面的代码,我得到这个错误: ((__t = ( firstName )) == null ? '' : __t) +

任何想法,我在做什么错了? 另外,应该只有一个为所有dynamic字段编译的模板?

 var firstName = _(contactinfo).pluck('firstName'); var compiledFirst = _.template('template with <%= firstName %>!'); var htmlFirst = compiledFirst(firstName); var lastName = _(contactinfo).pluck('lastName'); var compiledLast = _.template('template with <%= lastName %>!'); var htmlLast = compiledLast(lastName); var data = { from: store@example.com, to: email, subject: 'Your Order Confirmation', html: '<p>Dear '+ htmlFirst + htmlLast+': '</p><br> <p>Thank you for your order. . . </p><table><tr> <thead><th><strong>Items</strong></th></thead></tr></table>' } 

这是数组的样子:

 [ { "address": "555 Broadway", "city": "New York", "email": "johndoe@example.com", "firstName": "John", "lastName": "Doe", "phone": "2125551212", "state": { "code": "NY", "state": "New York" }, "value1": true, "zip": "10001", "$id": "-K-qmfZzHgQaEM7uHKEK", } ] 

请注意, pluck返回数组中的所有对象的firstName (或selected属性)。 不仅如此,还必须在模板中命名对象:

 var firstName = _(contactinfo).pluck('firstName'); var compiledFirst = _.template('template with <%= firstName %>!'); var htmlFirst = compiledFirst({firstName: firstName[0]}); 

只需要一个模板并将contactInfo对象传递给它可能会更容易:

 var htmlAll = _.template('<%= firstName %> <%= lastName %>')(contactInfo[0]) 

请记住,这只获取第0个contactInfo条目。 如果你想要不止一个,你可能需要迭代 – 但你也可以在lodash模板中做到这一点。