0

Hi I am making an express webapp connected to a mysql server on a remote db provided by my college. I am following a tutorial to set up passport.js authentification and have run into a problem when trying to post the data on posting the /regnew request

the error showing up in putty is :

Error: /students/danu7_tc3/project/views/error.hbs: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (/students/danu7_tc3/project/node_modules/express/lib/response.js:767:10) at ServerResponse.send (/students/danu7_tc3/project/node_modules/express/lib/response.js:170:12) at done (/students/danu7_tc3/project/node_modules/express/lib/response.js:1004:10) at /students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:93:9 at Object.done (/students/danu7_tc3/project/node_modules/hbs/lib/async.js:74:20) at /students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:88:18 at /students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:69:11 at Object.done (/students/danu7_tc3/project/node_modules/hbs/lib/async.js:74:20)

This is my index.js code

var express = require('express');
var router = express.Router();

var passport = require('passport');

var bcrypt = require('bcrypt');
const saltRounds = 10;


/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', {
        title: 'Express'
    });
});

router.get('/home', function(req, res, next) {
    res.render('home', {
        title: 'Home'
    });
});


router.get('/registration', function(req, res, next) {
    res.render('registration');

});
router.post('/regnew', function(req, res, next) {

    var username = req.body.username;
    var email = req.body.email;
    var password = req.body.password;


    console.log(username);


    const db = require('./db_connection.js');

    bcrypt.hash(password, saltRounds, function(err, hash) {
        db.query('INSERT INTO Users (username, email, password) VALUES 
            ( ? , ? , ? )
            ',[username,email,hash], function(error,result,fields) {
                if (error) throw error;

                db.query('SELECT LAST_INSERT_ID() as user_id', function(error,
                    results, fields) {
                    if (error) throw error;

                    const user_id = results[0];


                    console.log(results[0]);
                    req.login(user_id, function(err) {
                        res.redirect('/home');
                    });



                });
                res.render('registration', {
                    title: 'Success'
                });
            });
    });

});
passport.serializeUser(function(user_id, done) { //store user id in 
    session
    done(null, user_id);
});

passport.deserializeUser(function(id, done) { //read from the session

    done(err, user_id);
});



module.exports = router;
ct234
  • 15
  • 6
  • Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Ayush Gupta May 19 '19 at 16:50
  • Thank you that worked. It was because I had the response for the post request still there from before i added passport – ct234 May 19 '19 at 17:00

1 Answers1

0

You are calling res twice.

  1. res.redirect
  2. res.render
yeya
  • 1,968
  • 1
  • 21
  • 31