Nodejs window undefine

Viewed 170

How to solve ReferenceError: window is not defined? this is my code

  let timestamp = new Date();
  var str = params.email;
  var enc = window.btoa(str);
  var dec = window.atob(timestamp);
  console.log(window)
  if (window == "undefine"){
    var template = handlebars.compile(html);
    var htmlToSend = template({email:enc + "|" + dec});
    var mailOptions = {
      from: 'support@google.com',
      to: params.email,
      subject: 'Sending Email using Node.js',
      html: htmlToSend
    };

enter image description here

3 Answers

Your NodeJS code is server-side. You can't have the server code directly call client code, access its variables, and such; nor can you do it the other way around.

Alternate of btoa in nodejs

  let timestamp = new Date();
  var str = params.email;
  // create a buffer
  const buff = Buffer.from(str, 'utf-8');
  // decode buffer as Base64
  const base64 = buff.toString('base64');

  //var enc = window.btoa(str);
  //var dec = window.atob(timestamp);

Reference

Adding to what @Nabeel said, use this : if(typeof window === "undefined")

window is a browser thing that doesn't exist on Node.

Related