ExpressJS throwing multiple header error while I seem to be sending only one header

I’ve got the following block of code:

router.put("/:_id", async (req: Request, res: Response) => {

try {
    // Create the updated artist variable
    const artist: IArtist = req.body;

    const updatedArtist = await Artist.findOneAndUpdate({_id: req.params._id}, artist);

    return res.sendStatus(200).json(updatedArtist);
} catch (err) {
    // console.log(err);
    return res.sendStatus(400).send("Couldn't update artist");
}

});

This throws the following error:

(node:32408) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (C:UsersthimoDocumentsGitHubFlexTixservernode_modulesexpresslibresponse.js:771:10)  
    at ServerResponse.contentType (C:UsersthimoDocumentsGitHubFlexTixservernode_modulesexpresslibresponse.js:599:15)
    at ServerResponse.sendStatus (C:UsersthimoDocumentsGitHubFlexTixservernode_modulesexpresslibresponse.js:357:8)
    at C:UsersthimoDocumentsGitHubFlexTixserversrcroutesartists.ts:44:20
    at step (C:UsersthimoDocumentsGitHubFlexTixserversrcroutesartists.ts:33:23)
    at Object.next (C:UsersthimoDocumentsGitHubFlexTixserversrcroutesartists.ts:14:53)
    at fulfilled (C:UsersthimoDocumentsGitHubFlexTixserversrcroutesartists.ts:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

As far as I know, I’m only sending one header. How could I fix this problem?

Answer

You need to use .status() instead of .sendStatus(). When you use .json() or .send() on top of .sendStatus() – express tries to set additional headers on top of response that is already sent due to .sendStatus()