0

I would like to ask why am I keep getting the error which will occur if you send back more than 1 response:

events.js:298
      throw er; // Unhandled 'error' event
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:535:11)
    at ServerResponse.header (C:\Web Develope\node_modules\express\lib\response.js:771:10)
    at ServerResponse.send (C:\Web Develope\node_modules\express\lib\response.js:170:12)
    at C:\Web\wiki-API\app.js:69:21
    at C:\Web\node_modules\mongoose\lib\model.js:4798:16
    at C:\Web\node_modules\mongoose\lib\query.js:4389:12
    at C:\Web\node_modules\mongoose\lib\helpers\query\completeMany.js:35:39
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
    at C:\Web\node_modules\mongoose\lib\model.js:4800:13
    at C:\Web\node_modules\mongoose\lib\query.js:4389:12
    at C:\Web\node_modules\mongoose\lib\helpers\query\completeMany.js:35:39
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Here is my code:

app.route('/data/:dataName')
    .get((req,res)=>{
        Article.find({},(err,list_data)=>{
            if(!err){
                if(list_data){
                    list_data.forEach((item)=>{
                        if(_.lowerCase(req.params.dataName) === _.lowerCase(item.title)){
                            return res.send(item);
                        }
                    });
                }
                res.send("No data found!");
            }
        })
    })

I still can't figure out why after executing the 1st response it's still go to the next one and pop the error!

Thank you!

JadenB
  • 57
  • 2
  • 9
  • You can check [https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client?rq=1](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client?rq=1) – dangeneffy Feb 04 '20 at 09:16
  • Sweet! I'll look into it. – JadenB Feb 04 '20 at 09:32

2 Answers2

0

In Node Js you can only send one response at one request. You can't send two response if the user is found or not.

if(_.lowerCase(req.params.dataName) === _.lowerCase(item.title)){
                            return res.send(item);
                        }
else 
res.send("No data found!");
Engineer S. Saad
  • 378
  • 4
  • 19
0

The error you got is because of the response send multiple times.You should use error-first approach to handle your callback like following.

app.route('/data/:dataName')
.get((req,res)=>{
    Article.find({}, async (err,list_data)=>{
        if(err){
            return res.send("No data found!");
        }
        if(list_data){
           let filter_data = await list_data.filter((item)=>{
               return req.params.dataName === _.lowerCase(item.title)
           });
         return res.send(filter_data)
        }
    })
})
Sumit Kumar
  • 763
  • 9
  • 14