0

(node:10096) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:470:11) at ServerResponse.header (C:\Users\niko\Desktop\opaa\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\niko\Desktop\opaa\node_modules\express\lib\response.js:170:12) at exports.login (C:\Users\niko\Desktop\opaa\controllers\controller.js:32:27) at process._tickCallback (internal/process/next_tick.js:68:7)

const User = require('../model/User');
const bcrypt = require('bcryptjs')

exports.renderHomePage = (req, res, next) => {
    res.render('index') 
};

exports.register = async (req, res, next) => {
    const emailExist = await User.findOne({email: req.body.email});
    //if (emailExist) return res.send('Email Exist')
 
    res.render('register')

    const hash = await bcrypt.genSalt(10);
    const hashPassword = await bcrypt.hash(req.body.pass, hash);

    const user = new User({
        email: req.body.email,
        name: req.body.name,
        pass: hashPassword
        })
    try{
        const savedUser = await user.save();
    }catch(err){
        
    }
};

exports.login = async (req, res, next) => {
    res.render('login')
    const user = await User.findOne({ email: req.body.email1 });
    if (!user) return res.send("Invalid Email");

    email1 = req.body.email1;
    pass1 = req.body.pass1;


    const validPass = await bcrypt.compare(pass1, user.pass);
    if (!validPass) return res.send('Invalid Password')
    return res.redirect('/home');

};
  • Does this answer your question? [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 Jul 03 '21 at 15:51
  • Why are you doing `res.render('login')`? – Ayush Gupta Jul 03 '21 at 15:52

0 Answers0