'express server error Cannot set headers after they are sent to the client

I am using a react frontend and sending form data to a express server backend. I have managed to send and receive this data however only in console.log form. The contents of this from is being sent in json and is viewable in a console.log however I need this info in variables to use in a script and whenever I try put the data into a variable to use, I get the error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

my code is

var express = require('express');
var router = express.Router();
const bodyParser = require('body-parser');
const { response } = require('../app');
const { json } = require('express/lib/response');

const app = express();

var balls

//app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// app.post('/idle', (req, res) => {
//   console.log('got body', req.body);
//   res.sendStatus(200);
//   console.log(req.body);
//   console.log('big nuts ' + balls)
// })

/* GET home page. */
router.get('/', function(req, res) {
    res.render('idle', { title: 'idle get placeholder' });
});

var body

function getKeyByValue(object, value) {
    return Object.keys(object).find(key => object[key] === value);
}
  

/* POST home page. */
router.post('/', function(req, res) {
    console.log(req.body)
    //not worky 
    //console.log(getKeyByValue(req.body, 'accName'))
    //console.log(Object.keys(req.body))
    //res.send(res.body)
    //console.log(req.body + 'log');
    res.render('idle', { title: 'idle post placeholder' });
    //obj = JSON.parse(req.body)
    //body = req.body
    // password = req.body[password]
    // steamGuardCode = req.body[steamGuard]
    // console.log(accName + password + steam)   

});
module.exports = router;

sorry about the variable names. I name them that for testing and forget to replace them also I kind of left all my test fixes in there so anything commented doesn't work



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source