Assignment to Constant Variable

i try to read the user input and sent it as a email. but when i run this code it gives me this error: Assignment to constant variable.

Any help will be appreciate

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 <>'
    }

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

})

module.exports = router

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest