how do i redirect from apppost to nodejs?

Your error sounds like you have refined the symbol res so that it isn’t the res from the app.post() callback arguments, but rather some other res. It seems that perhaps your actual code is this:

app.post('/submit', (req, res) => {
  console.log(req.body);
  var sql = "insert into `members` (`email`, `type`, `name`, `uid`) values ('"+ req.body.email +"', '"+ req.body.type +"', '"+req.body.name+"', '"+req.body.uid+"')";
    db.query(sql, (err, res) => {
        if (err) throw err;
        if (req.body.type == 'student') {
            res.redirect('/student');  
        } else {
            res.redirect('/teacher');
        }
    })  
})

Where the res in this db.query(sql, (err, res) => { overrides the higher scoped res in app.post('/submit', (req, res) => { and you get the wrong res when you try to do res.redirect().

The fix is to make sure you use a different argument name in db.query(sql, (err, qres) => {. Since the code in your actual question shows res1 as that argument, that code should work as you show, but apparently that’s not what is in your actual code.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top