Friday, 12 April 2019

Error ' Cannot set headers after they are sent to the client' for GET request on nodejs

I am building a restful api on nodejs. my request runs fine on postman but whenever i try to get just one item, my cmd crashes with this errorCannot set headers after they are sent to the clientthis is the codegetTodo(req, res) {const id = parseInt(req.params.id, 10);db.map((todo) => {if (todo.id === id) {return res.status(200).send({success: 'true',message: 'todo retrieved successfully',todo,});}});return res.status(404).send({success: 'false',message: 'todo does not exist',});}this is my package.json{"scripts": {"test": "mocha --require @babel/register tests/*.js --exit","build": "babel app.js --out-dir build","dev-start": "babel-watch app.js"},"author": "Odunayo Okebunmi","license": "ISC","dependencies": {"body-parser": "^1.18.3","chai": "^4.2.0","chai-http": "^4.2.1","eslint": "^5.16.0","eslint-config-airbnb-base": "^13.1.0","eslint-plugin-import": "^2.16.0","express": "^4.16.4"},"devDependencies": {"@babel/cli": "^7.4.3","@babel/core": "^7.4.3","@babel/preset-env": "^7.4.3","@babel/register":"^7.4.0","babel-watch": "^7.0.0","expect": "^24.7.1","mocha": "^6.1.3","nodemon": "^1.18.11","supertest": "^4.0.2"}}

Submitted April 12, 2019 at 11:45PM by PearllyO

No comments:

Post a Comment