success ? 'sent' : 'failed' defaults to 'sent' for success = null. Why?
-
20-04-2021 - |
Question
I'm playing around with emailing from nodeJS (using the nodemailer lib), and I'm currently hitting some timeouts on the whole mailing process. That's not the problem I'd need help with. The problem I do need help with is that success will be null when it hits the logging part, and sole.log('Message ' +that makes the whole console.log statement to output "sent". No "Message", no failed.
Any idea why?
nodemailer.send_mail(
// e-mail options
{
to:"alexsb92@gmail.com",
sender:"alexsb92@gmail.com",
subject:"node_mailer test email",
html:'<p><b>Hi,</b> how are you doing?</p>',
body:'Hi, how are you doing?'
},
// callback function
function(error, success){
console.log('Message ' + success ? 'sent' : 'failed');
}
);
Solution
Operator precedence? Try:
'Message ' + (success ? 'sent' : 'failed')
OTHER TIPS
It's your priority of operations. The +
operator has higher precedence than the ternary operator, and as such, you're actually doing
console.log(('Message ' + success) ? 'sent' : 'failed');
Which is always true. Instead, do:
console.log('Message ' + (success ? 'sent' : 'failed'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow