Unable to res.send in express

Issue

Hello I am having issue sending response with express.

I have read in other questions that maybe changing the err, res to other variables may fix this issue however it doesn’t.

router.post('/checkPaid', (req, res, next) => {
  db.query(`SELECT email_address FROM users WHERE email_address = ?`, req.body.email_address, (err, res) => {
    if(err)
    {
      throw err
    }
    else
    {
      console.log('User found')
      db.query(`SELECT course_paid FROM users WHERE email_address = ?`, req.body.email_address, (error, results) => {
        if(error)
        {
          console.log(error)
        }
        else
        {
          results.send('test')
        }
      })
    }
  })
})

 throw err; // Rethrow non-MySQL errors
      ^

TypeError: results.send is not a function

Solution

Try using res.send(‘test’) instead of results.send(‘test’), because res is express response object.

EDIT:

router.post('/checkPaid', (req, res, next) => {
  db.query(`SELECT email_address FROM users WHERE email_address = ?`, req.body.email_address, (err, results) => {
    if(err)
    {
      throw err
    }
    else
    {
        console.log('User found')
        db.query(`SELECT course_paid FROM users WHERE email_address = ?`, req.body.email_address, (error, results) => {
            if(error)
            {
                console.log(error)
            }
            else
            {
                res.send('test')
            }
      })
    }
  })
})

Answered By – Amine

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published