node JS erorr in my app (Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client)

Osama Sheha

When I type nodeJS I get this error in my application please help me. , When I run the app it works, when I send the first POST it gives me this error

const express = require('express')
const Workout = require("../models/workoutModel")

const router = express.Router()

// Get all workouts
router.get('/',(req,res) => {
    res.json({mssg:'GET all workouts'})
})

// Get a single workouts
router.get('/:id',(req,res) => {
    res.json({mssg: 'GET a single workout'})
})

// POST a new workout
router.post('/', async (req,res) => {
    const {title,load,reps} = req.body

    try {
        const workout = await Workout.create({title,load,reps})
        res.status(200).json(workout)
    } catch(error) {
        res.status(400).json({error: error.message})
    }
    res.json({mssg: 'POST a new workout'})
})

// DELETE a workout
router.delete('/:id',(req,res) => {
    res.json({mssg: 'DELETE a workout'})
})

// UPDATE a workout
router.patch('/:id',(req,res) => {
    res.json({mssg: 'UPDATE a workout'})
})


module.exports = router

When I run the app it works, when I send the first POST it gives me this error.

Simba

you sent response in try-catch, so res.json({mssg: 'POST a new workout'}) cause to error. delete res.json({mssg: 'POST a new workout'}) for solving error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client [node and express js]--

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

Node js POST Request error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Node.js Error when testing with postman (Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client)

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Node Express Mongodb

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Node.Js, Express, Postgres

Express JS - Redirecting If MongoDB id doesn't exist / Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I am getting this error : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

showing error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client on terminal

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

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Express + Request

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

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

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

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client when adding response

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

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

Getting "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" when trying to redirect

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

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - How to solve it

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, when making request to API

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

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

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client | NodeJS ExpressJS MySQL

[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Error when trying request

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader

Why I am getting : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

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