node.jsnodemailer

Assignment to constant variable


I try to read the user input and send it as a email. But when I run this code it gives me this error: Assignment to constant variable.

var mail= require('./email.js')
var express = require('express')
var router = express.Router()

router.post('/',function(req, res, next){
    var address = req.fields.address
    var text = req.fields.text
    var subject = req.fields.subject

    try{
       if(text = 0){
           throw new Error('Please enter what u want to say')
       }
       if(subject = 0){
           throw new Error('Please enter subject')
       }

    }catch(e){
        req.flash('error', e.message)
        return res.redirect('back')
    }

    var detail = {
        to:address,
        text:text,
        subject:subject,
        from: 'test <nbuudilc@126.com>'
    }

    email(detail).then(function(){
        req.flash('success','email sent success')
        res.redirect('/posts')
    })

})

module.exports = router

Solution

  • You might want:

    if (text == 0)
    

    and:

    if (subject == 0)
    

    or:

    if (!text)
    

    and:

    if (!subject)
    

    I would think the latter option is better, at least stylistically.