sandra27
sandra27

Reputation: 27

Cannot send email using switch statement

I'm using node mailer to send out the emails and have followed a youtube tutorial, but I did some changes. I want to the user to send an email to any of the 31 people the he/she will choose, and so we will call the number assigned to people being chosen is classNum.

When I assign an email to the to parameter, the code works, but if I remove it and use the switch statement, it give the 500 error.

const sendMail = (email, subject, text,classNum, cb) => {
const mailOptions = {
  from: email,
  subject,
  text,
  to: " "
};
switch(classNum) {
  case 25:  
    mailOptions.to = '[email protected]'          
    break;
}

This is probably the chunk of code that has a problem, please tell me if I need to add/post more code from the source code.

Upvotes: 1

Views: 121

Answers (2)

Matt S
Matt S

Reputation: 86

As @epascarello indicated, you have a string in classNum, not a number; as a result, you "case" doesn't match. You can fix this one of two ways:

  1. Make sure classNum is a number, rather than a string (probably better, since classNum implies a number in the variable name) by using classNum = Number(classNum); OR
  2. change your switch case to be a string: case '25':

I think option 1 is probably better, again because the variable name implies a number value.

Upvotes: 1

epascarello
epascarello

Reputation: 207557

A switch expression uses strict equality

With the debugging info I asked for in the comments, you are passing in a string and you are comparing it against a number. So "25" === 25 is false.

You either need to make it a string

switch(classNum) {
  case "25": 

or convert classNum to a number

switch(Number(classNum)) {
  case 25: 

Upvotes: 2

Related Questions