D
D
DollyPapper2021-05-24 11:45:58
JavaScript
DollyPapper, 2021-05-24 11:45:58

Error sending express http response after request to database?

Colleagues, hello.
There is such a controller (it makes no sense, just indicative).

async register(req, res) {
    
    try {
      
      await UserModel.findAll();
      console.log('THIS');
      res.send('THIS');
    }catch (e) {
      res.send('ERROR');
      console.log('ERROR CATCHING');
    }


When requesting through sikalize to the database, if you make a call to the findAll function through await, we get the following error
spoiler

node:internal/process/promises:245
triggerUncaughtException(err, true /* fromPromise */);
^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:573:11)
at ServerResponse.header (/Users/denisdavydov/Desktop/espp_service/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/Users/denisdavydov/Desktop/espp_service/node_modules/express/lib/response.js:170:12)
at register (/Users/denisdavydov/Desktop/espp_service/controller/User.js:31:11)
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
code: 'ERR_HTTP_HEADERS_SENT'

It's like something implicitly already set the title. If you make a request synchronously, then the response is sent to the client. The request itself fulfills, console.log after it fulfills (displays THIS), but res.send("THIS") already crashes with the above error. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DollyPapper, 2021-05-24
@DollyPapper

Issue resolved. By my own stupidity, in the middleware, when checking the connection to the database, I called next () twice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question