klaurtar
klaurtar

Reputation: 253

How to fix nodemailer "object object" error in node.js website?

I am creating a node.js website for a business and would like to be able to notify through email, everytime someone applies. I am using nodemailer and mailgun to send an email every time the job application form is submitted. The emails are being sent, however, it does not contain the key value pairs of the applicant object I've created. Any help would be greatly appreciated!

Here is an image of the email I receive when submitting and application enter image description here

Here is the nodemailer code I'm running

const nodemailer = require('nodemailer');
const mailgun = require('nodemailer-mailgun-transport');
const debug = require('debug')('app:mail');

const auth = {
    auth: {
       api_key: '**************',
       domain: '***************' 
    }
};

const transporter = nodemailer.createTransport(mailgun(auth));

function sendOrderEmail(applicant) {
  let html = '<ul>';

  Object.entries(applicant).forEach(([key, value]) => {
    html += `<li>${key}: ${value}</li>`;
  });

  html += '</ul>';

  const mailOptions = {
    from: '*************',
    to: '*********, *************',
    subject: '*****************',
    html
  };

  transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      debug(`Error: ${err}`);
    } else {
      debug(`Info: ${info}`);
    }
  });
}

module.exports = sendOrderEmail;

Here is my post route where I create the applicant object

app.post('/employment', function(req, res){

    var firstName = req.body.firstName;
    var middleInitial = req.body.middleInitial;
    var lastName = req.body.lastName;
    var address = req.body.address;
    var city = req.body.city;
    var state = req.body.state;
    var zipCode = req.body.zipCode;
    var phoneNumber = req.body.phoneNumber;
    var doYouRecieveText = req.body.doYouRecieveText;

    var newApplicant = {
        firstName: firstName,
        middleInitial: middleInitial,
        lastName: lastName,
        address: address,
        city: city,
        state: state,
        zipCode: zipCode,
        phoneNumber: phoneNumber,
        doYouRecieveText: doYouRecieveText
    };

    Applicant.create(newApplicant, function(err, newlyCreated){
        if(err) {
            console.log(err);
        } else {
            console.log(newlyCreated);
            sendOrderEmail(newlyCreated);
            res.redirect('/');
        }
    });
});

Upvotes: 0

Views: 681

Answers (2)

klaurtar
klaurtar

Reputation: 253

I was passing through the newlyCreated parameter to the sendOrderEmail function when I should have been passing through the newApplicant variable

Applicant.create(newApplicant, function(err, newlyCreated){
    if(err) {
        console.log(err);
    } else {
        console.log(newlyCreated);
        sendOrderEmail(newApplicant);
        res.redirect('/');
    }
});

Upvotes: 0

Hitesh Lala
Hitesh Lala

Reputation: 336

It looks like the value you are attempting to insert in your html is an Object but the html is expecting a value of type String.

Try stringifying your value before inserting it in your html.

html += `<li>${key}: ${ typeof value === 'string' ? value : JSON.stringify(value)}</li>`;

Upvotes: 2

Related Questions