forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Locals loosing dynamic defined Properties #344

Closed JSchatral closed 5 years ago

JSchatral commented 5 years ago

Hello, here my Developer environment:

First method - this produce an email without any errors var optionsB = { template: path.join(dirname,'../templates','contact'), message: { to: email, subject: subject,
attachments: [{ filename: 'home-showcase.png', path: path.join(
dirname,'../templates','contact/home-showcase.png'), cid: 'home-showcase' }]
}, locals: {
Fa_Plz: row['Fa_Plz'], Fa_Ort: row['Fa_Ort'], Fa_Land: row['Fa_Land'], name: Empfaengername, token: tokenUrl }
};

Second method: I wanted to get the vars dynamically from a Database Table and then do following - this is it the crux of the matter:

locals: target

--> here is the code: connection.query(queryoptions, function (error, rows, fields) { connection.release(); if (error) throw error; var row = rows[0];
var target={};

        for (let i = 0; i < fields.length; i++) {            
            Object.defineProperty(target, fields[i].name, {
            value: row[fields[i].name]               
          });           
        }

        console.log(target);  // its ok - all my properties are in there

// now I use the ES6 Object.assign --->

var options = Object.assign( { template: path.join(dirname,'../templates','contact'), message: { to: email, subject: subject,
attachments: [{ filename: 'home-showcase.png', path: path.join(
dirname,'../templates','contact/home-showcase.png'), cid: 'home-showcase' //same cid value as in the html img src }]
}, locals: target // this is it the crux of the matter
});

// but this produce an email with an "referenceError". To catch this error I debug it with VS Code:

... now a peace of code you see as an index.js source file produced in Debug mode with VS Code - I think you know where to find it in your developer sources...

async send(options = {}) { options = Object.assign( { template: '', message: {}, locals: {} }, options ); // After that statement "locals" do have all the properties - nothing is gone until now: // Fa_WebShopAdmin:"Admin@Kunstgalerie-Hieke.de" // Fa_WebShopInfo:"info@Kunstgalerie-Hieke.de" // ... and so on let { template, message, locals } = options; // here again nothing was loosing all dynamic generated properties I can view in console.log().

const attachments =
  message.attachments || this.config.message.attachments || [];

message = _.defaultsDeep(
  {},
  _.omit(message, 'attachments'),
  _.omit(this.config.message, 'attachments')
);

locals = _.defaultsDeep({}, this.config.views.locals, locals); 

// after that line - I show me "locals" again in debug-mode: only one Object property in "local {pretty: true}" all other properties are loosing.

Is this a node.js issue or a miss understandig of mine? C'ant I do any dynamic property allocation in this case?

Greetings from Germany - Jürgen Schatral