0

node js thunder when I send multiple requests to the server at the same time, but not when I send one by one. And nodejs says.

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

(node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

That error in common when you send two responses to the client but in my code only sent one

This is my code

const User = require('../../../modelos/users');

async function getAllValuationsByUserName(req, res, next) {

  let isOwner = false;

  const userId = req.params.id;

  const pagination = {
    skip: Number(req.query.skip),
    limit: Number(req.query.limit)
  }



  if (userId === res.locals.userid) {
    isOwner = true;
  }

  try {

    const user = await User.findOne(
        { userName: new RegExp('^' + userId + '$', "i") },
        {
            'userPhoto.valuations': {
                $slice: [pagination.skip, pagination.limit]
            }
        })

    const valuations = user.userPhoto.valuations
    const total = user.userPhoto.valuations.length

    return res.status(200).send({
        erro: false,
        isOwner,
        valuations,
        total
    })


  } catch (err) {
    console.log(err)
    return res.status(400).send({
        error: true,
        inf: err
    });

  }

}

module.exports = getAllValuationsByUserName
Andrew Nolan
  • 1,987
  • 2
  • 20
  • 23
  • Duplicate https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client – Hiteshdua1 Dec 31 '18 at 04:34
  • That error comes about when you use the same request object twice – Mikkel Dec 31 '18 at 04:41
  • Took a quick look at your code, looks good to me. Can you please post the complete snippet if there is any more code snippet associated to it. – Sridhar Dec 31 '18 at 11:12

0 Answers0