safak / youtube

5.04k stars 5.47k forks source link

node:internal/errors:464 ErrorCaptureStackTrace(err); #54

Open TausifM opened 2 years ago

TausifM commented 2 years ago

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_HTTP_HEADERS_SENT' }

Joniekesh commented 2 years ago

Always endeavor to write the 'return' keyword before every response 'res' except the last one.

For instance let me demonstrate with Login authentication below:

const user = await User.findOne({email)};

if(!user) { return res.status(400).json({ msg: 'Invalid Credentials'}); }

const isMatch = await bcrypt.compare(user.password, password);

if(!isMatch) { return res.status(400).json({ msg: 'Invalid Credentials''}); }

res.status(200).json(user)

If you omit the 'return' keyword of the subsequent response before the last response you will get the error.

You can omit the last 'res' and it will work.

Oroyee commented 1 year ago

@Joniekesh Thank you so much!!! this problem was a pain in my ass for long days. now it solved:)))