Error: Setting header after it is sent - Help me understand why?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I don't get how I'm setting a header after it is sent to client?



code: During form submission a post ajax request is made, the response is a json object that is returned to the client.



I am commented out most of the code to find out why, at this point I am only doing one res.json() after a post request is made. I don't understand at what point I am RE-SETTING the header/ or returning a reponse twice?



YET I get this error:




Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:170:12)
at done (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1004:10)
at Object.exports.renderFile (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:412:12)
at View.exports.__express [as engine] (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:455:11)
at View.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/view.js:135:8)
at tryRender (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:640:10)
at Function.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1008:7)



index.js



var express = require('express');
var router = express.Router();

let users = ;



/* GET home page. */
router.get('/', function(req, res, next) {

res.render('index', { title: 'Express' });
});

router.post('/', function(req, res, next) {
// check post valued are true
if(req.body.fname && req.body.list_name && req.body.list_date) {
// try {
addNewUser(req.body);
res.json({
"message":"Successfully signed up!",
"userName": req.body.fname,
"userDate": req.body.list_date
});
// } catch(e){
// signup failed
// res.json(res.status(status).json(obj), "Sign up failed...");
// }
}
// if(req.body.list_item) {
// console.log(req.body.list_item);
// }
console.log(body.req);

});
module.exports = router;

function addNewUser(data) {
try {
users.push(
{
"fname":data.fname,
"list_name": data.list_name,
"list_date": data.list_date
}
);
} catch(e) {
console.log("Failed to add user ", e);
throw e;
}

}


myscript.js --- front end client side client



document.getElementById("createNewList").addEventListener('submit', function(e) {
e.preventDefault();

// serialize form to convert into JSOn object
let form_data = formSerialize(document.getElementById("createNewList"));

// AJAX fetch()
fetch('/', {
method: 'POST',
headers: {
"Content-Type": "application/json; charset=utf-8",
// "Content-Type": "application/x-www-form-urlencoded",
},
body: form_data
})
.then(function(response) {
if(response.ok)
return response.json();
})
.then(function(user_json) {
console.log(user_json);

console.log(user_json);
create_todays_todo_list(user_json);
});
});

// add event listener of the to do list i.e. adding tasks deleting tasks, completed tasks
// document.getElementById("toDoList_form").addEventListener('submit', function(e) {
// e.preventDefault();
// console.log("clikeddddddddd");
// // AJAX send added task to server
// let list_item = document.getElementById("list_item").innerHTML;
// let user = document.getElementById("user_name_heading").innerHTML;
// let data = {"user": user, "list_item": list_item}
// console.log(data);
// AJAX fetch()
// fetch('/', {
// method: 'POST',
// headers: {
// "Content-Type": "application/json; charset=utf-8",
// // "Content-Type": "application/x-www-form-urlencoded",
// },
// body: data
// })
// .then(function(response) {
// if(response.ok)
// return response.json();
// })
// .then(function(retrievedItemsFromServer) {
// console.log(retrievedItemsFromServer);
// });

//
// });


/// functionssss
// serialize form function
function formSerialize(form) {
var input = form.getElementsByTagName("input");
var formData = {};
for (var i = 0; i < input.length; i++) {
formData[input[i].name] = input[i].value;
}
return formData = JSON.stringify(formData);
}
function create_todays_todo_list(user_json) {
// 1. hide the create list form form
document.getElementById("createNewList").style.display="none";

// 2. display the to do list div
document.getElementById("toDoList").style.display="flex";

// 3. display the user's name and date as logged in on the page
document.getElementById("user_name_heading").innerHTML = user_json.userName;
document.getElementById("user_date_signup_heading").innerHTML = user_json.userDate;

}









share|improve this question





























    0















    I don't get how I'm setting a header after it is sent to client?



    code: During form submission a post ajax request is made, the response is a json object that is returned to the client.



    I am commented out most of the code to find out why, at this point I am only doing one res.json() after a post request is made. I don't understand at what point I am RE-SETTING the header/ or returning a reponse twice?



    YET I get this error:




    Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:170:12)
    at done (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1004:10)
    at Object.exports.renderFile (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:412:12)
    at View.exports.__express [as engine] (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:455:11)
    at View.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1008:7)



    index.js



    var express = require('express');
    var router = express.Router();

    let users = ;



    /* GET home page. */
    router.get('/', function(req, res, next) {

    res.render('index', { title: 'Express' });
    });

    router.post('/', function(req, res, next) {
    // check post valued are true
    if(req.body.fname && req.body.list_name && req.body.list_date) {
    // try {
    addNewUser(req.body);
    res.json({
    "message":"Successfully signed up!",
    "userName": req.body.fname,
    "userDate": req.body.list_date
    });
    // } catch(e){
    // signup failed
    // res.json(res.status(status).json(obj), "Sign up failed...");
    // }
    }
    // if(req.body.list_item) {
    // console.log(req.body.list_item);
    // }
    console.log(body.req);

    });
    module.exports = router;

    function addNewUser(data) {
    try {
    users.push(
    {
    "fname":data.fname,
    "list_name": data.list_name,
    "list_date": data.list_date
    }
    );
    } catch(e) {
    console.log("Failed to add user ", e);
    throw e;
    }

    }


    myscript.js --- front end client side client



    document.getElementById("createNewList").addEventListener('submit', function(e) {
    e.preventDefault();

    // serialize form to convert into JSOn object
    let form_data = formSerialize(document.getElementById("createNewList"));

    // AJAX fetch()
    fetch('/', {
    method: 'POST',
    headers: {
    "Content-Type": "application/json; charset=utf-8",
    // "Content-Type": "application/x-www-form-urlencoded",
    },
    body: form_data
    })
    .then(function(response) {
    if(response.ok)
    return response.json();
    })
    .then(function(user_json) {
    console.log(user_json);

    console.log(user_json);
    create_todays_todo_list(user_json);
    });
    });

    // add event listener of the to do list i.e. adding tasks deleting tasks, completed tasks
    // document.getElementById("toDoList_form").addEventListener('submit', function(e) {
    // e.preventDefault();
    // console.log("clikeddddddddd");
    // // AJAX send added task to server
    // let list_item = document.getElementById("list_item").innerHTML;
    // let user = document.getElementById("user_name_heading").innerHTML;
    // let data = {"user": user, "list_item": list_item}
    // console.log(data);
    // AJAX fetch()
    // fetch('/', {
    // method: 'POST',
    // headers: {
    // "Content-Type": "application/json; charset=utf-8",
    // // "Content-Type": "application/x-www-form-urlencoded",
    // },
    // body: data
    // })
    // .then(function(response) {
    // if(response.ok)
    // return response.json();
    // })
    // .then(function(retrievedItemsFromServer) {
    // console.log(retrievedItemsFromServer);
    // });

    //
    // });


    /// functionssss
    // serialize form function
    function formSerialize(form) {
    var input = form.getElementsByTagName("input");
    var formData = {};
    for (var i = 0; i < input.length; i++) {
    formData[input[i].name] = input[i].value;
    }
    return formData = JSON.stringify(formData);
    }
    function create_todays_todo_list(user_json) {
    // 1. hide the create list form form
    document.getElementById("createNewList").style.display="none";

    // 2. display the to do list div
    document.getElementById("toDoList").style.display="flex";

    // 3. display the user's name and date as logged in on the page
    document.getElementById("user_name_heading").innerHTML = user_json.userName;
    document.getElementById("user_date_signup_heading").innerHTML = user_json.userDate;

    }









    share|improve this question

























      0












      0








      0








      I don't get how I'm setting a header after it is sent to client?



      code: During form submission a post ajax request is made, the response is a json object that is returned to the client.



      I am commented out most of the code to find out why, at this point I am only doing one res.json() after a post request is made. I don't understand at what point I am RE-SETTING the header/ or returning a reponse twice?



      YET I get this error:




      Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:470:11)
      at ServerResponse.header (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:767:10)
      at ServerResponse.send (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:170:12)
      at done (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1004:10)
      at Object.exports.renderFile (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:412:12)
      at View.exports.__express [as engine] (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:455:11)
      at View.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/view.js:135:8)
      at tryRender (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:640:10)
      at Function.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:592:3)
      at ServerResponse.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1008:7)



      index.js



      var express = require('express');
      var router = express.Router();

      let users = ;



      /* GET home page. */
      router.get('/', function(req, res, next) {

      res.render('index', { title: 'Express' });
      });

      router.post('/', function(req, res, next) {
      // check post valued are true
      if(req.body.fname && req.body.list_name && req.body.list_date) {
      // try {
      addNewUser(req.body);
      res.json({
      "message":"Successfully signed up!",
      "userName": req.body.fname,
      "userDate": req.body.list_date
      });
      // } catch(e){
      // signup failed
      // res.json(res.status(status).json(obj), "Sign up failed...");
      // }
      }
      // if(req.body.list_item) {
      // console.log(req.body.list_item);
      // }
      console.log(body.req);

      });
      module.exports = router;

      function addNewUser(data) {
      try {
      users.push(
      {
      "fname":data.fname,
      "list_name": data.list_name,
      "list_date": data.list_date
      }
      );
      } catch(e) {
      console.log("Failed to add user ", e);
      throw e;
      }

      }


      myscript.js --- front end client side client



      document.getElementById("createNewList").addEventListener('submit', function(e) {
      e.preventDefault();

      // serialize form to convert into JSOn object
      let form_data = formSerialize(document.getElementById("createNewList"));

      // AJAX fetch()
      fetch('/', {
      method: 'POST',
      headers: {
      "Content-Type": "application/json; charset=utf-8",
      // "Content-Type": "application/x-www-form-urlencoded",
      },
      body: form_data
      })
      .then(function(response) {
      if(response.ok)
      return response.json();
      })
      .then(function(user_json) {
      console.log(user_json);

      console.log(user_json);
      create_todays_todo_list(user_json);
      });
      });

      // add event listener of the to do list i.e. adding tasks deleting tasks, completed tasks
      // document.getElementById("toDoList_form").addEventListener('submit', function(e) {
      // e.preventDefault();
      // console.log("clikeddddddddd");
      // // AJAX send added task to server
      // let list_item = document.getElementById("list_item").innerHTML;
      // let user = document.getElementById("user_name_heading").innerHTML;
      // let data = {"user": user, "list_item": list_item}
      // console.log(data);
      // AJAX fetch()
      // fetch('/', {
      // method: 'POST',
      // headers: {
      // "Content-Type": "application/json; charset=utf-8",
      // // "Content-Type": "application/x-www-form-urlencoded",
      // },
      // body: data
      // })
      // .then(function(response) {
      // if(response.ok)
      // return response.json();
      // })
      // .then(function(retrievedItemsFromServer) {
      // console.log(retrievedItemsFromServer);
      // });

      //
      // });


      /// functionssss
      // serialize form function
      function formSerialize(form) {
      var input = form.getElementsByTagName("input");
      var formData = {};
      for (var i = 0; i < input.length; i++) {
      formData[input[i].name] = input[i].value;
      }
      return formData = JSON.stringify(formData);
      }
      function create_todays_todo_list(user_json) {
      // 1. hide the create list form form
      document.getElementById("createNewList").style.display="none";

      // 2. display the to do list div
      document.getElementById("toDoList").style.display="flex";

      // 3. display the user's name and date as logged in on the page
      document.getElementById("user_name_heading").innerHTML = user_json.userName;
      document.getElementById("user_date_signup_heading").innerHTML = user_json.userDate;

      }









      share|improve this question














      I don't get how I'm setting a header after it is sent to client?



      code: During form submission a post ajax request is made, the response is a json object that is returned to the client.



      I am commented out most of the code to find out why, at this point I am only doing one res.json() after a post request is made. I don't understand at what point I am RE-SETTING the header/ or returning a reponse twice?



      YET I get this error:




      Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:470:11)
      at ServerResponse.header (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:767:10)
      at ServerResponse.send (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:170:12)
      at done (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1004:10)
      at Object.exports.renderFile (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:412:12)
      at View.exports.__express [as engine] (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/pug/lib/index.js:455:11)
      at View.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/view.js:135:8)
      at tryRender (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:640:10)
      at Function.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/application.js:592:3)
      at ServerResponse.render (/home/shahyan/Videos/FrontendProject-masters/notes-js/beginner/js-fundamentals-functional/todoApp/node_modules/express/lib/response.js:1008:7)



      index.js



      var express = require('express');
      var router = express.Router();

      let users = ;



      /* GET home page. */
      router.get('/', function(req, res, next) {

      res.render('index', { title: 'Express' });
      });

      router.post('/', function(req, res, next) {
      // check post valued are true
      if(req.body.fname && req.body.list_name && req.body.list_date) {
      // try {
      addNewUser(req.body);
      res.json({
      "message":"Successfully signed up!",
      "userName": req.body.fname,
      "userDate": req.body.list_date
      });
      // } catch(e){
      // signup failed
      // res.json(res.status(status).json(obj), "Sign up failed...");
      // }
      }
      // if(req.body.list_item) {
      // console.log(req.body.list_item);
      // }
      console.log(body.req);

      });
      module.exports = router;

      function addNewUser(data) {
      try {
      users.push(
      {
      "fname":data.fname,
      "list_name": data.list_name,
      "list_date": data.list_date
      }
      );
      } catch(e) {
      console.log("Failed to add user ", e);
      throw e;
      }

      }


      myscript.js --- front end client side client



      document.getElementById("createNewList").addEventListener('submit', function(e) {
      e.preventDefault();

      // serialize form to convert into JSOn object
      let form_data = formSerialize(document.getElementById("createNewList"));

      // AJAX fetch()
      fetch('/', {
      method: 'POST',
      headers: {
      "Content-Type": "application/json; charset=utf-8",
      // "Content-Type": "application/x-www-form-urlencoded",
      },
      body: form_data
      })
      .then(function(response) {
      if(response.ok)
      return response.json();
      })
      .then(function(user_json) {
      console.log(user_json);

      console.log(user_json);
      create_todays_todo_list(user_json);
      });
      });

      // add event listener of the to do list i.e. adding tasks deleting tasks, completed tasks
      // document.getElementById("toDoList_form").addEventListener('submit', function(e) {
      // e.preventDefault();
      // console.log("clikeddddddddd");
      // // AJAX send added task to server
      // let list_item = document.getElementById("list_item").innerHTML;
      // let user = document.getElementById("user_name_heading").innerHTML;
      // let data = {"user": user, "list_item": list_item}
      // console.log(data);
      // AJAX fetch()
      // fetch('/', {
      // method: 'POST',
      // headers: {
      // "Content-Type": "application/json; charset=utf-8",
      // // "Content-Type": "application/x-www-form-urlencoded",
      // },
      // body: data
      // })
      // .then(function(response) {
      // if(response.ok)
      // return response.json();
      // })
      // .then(function(retrievedItemsFromServer) {
      // console.log(retrievedItemsFromServer);
      // });

      //
      // });


      /// functionssss
      // serialize form function
      function formSerialize(form) {
      var input = form.getElementsByTagName("input");
      var formData = {};
      for (var i = 0; i < input.length; i++) {
      formData[input[i].name] = input[i].value;
      }
      return formData = JSON.stringify(formData);
      }
      function create_todays_todo_list(user_json) {
      // 1. hide the create list form form
      document.getElementById("createNewList").style.display="none";

      // 2. display the to do list div
      document.getElementById("toDoList").style.display="flex";

      // 3. display the user's name and date as logged in on the page
      document.getElementById("user_name_heading").innerHTML = user_json.userName;
      document.getElementById("user_date_signup_heading").innerHTML = user_json.userDate;

      }






      javascript node.js http express fetch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 10:51









      ShazShaz

      5751620




      5751620
























          1 Answer
          1






          active

          oldest

          votes


















          0














          console.log(body.req); this is wrong. It should be console.log(req.body);. This generally happens when the program exited but still, there is a function callback in the stack. This should fix your problem. If it didn't please share the console log if printed any






          share|improve this answer
























          • addNewUser() was below the router.post() which is why it wasn't working

            – Shaz
            Nov 28 '18 at 2:00












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466723%2ferror-setting-header-after-it-is-sent-help-me-understand-why%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          console.log(body.req); this is wrong. It should be console.log(req.body);. This generally happens when the program exited but still, there is a function callback in the stack. This should fix your problem. If it didn't please share the console log if printed any






          share|improve this answer
























          • addNewUser() was below the router.post() which is why it wasn't working

            – Shaz
            Nov 28 '18 at 2:00
















          0














          console.log(body.req); this is wrong. It should be console.log(req.body);. This generally happens when the program exited but still, there is a function callback in the stack. This should fix your problem. If it didn't please share the console log if printed any






          share|improve this answer
























          • addNewUser() was below the router.post() which is why it wasn't working

            – Shaz
            Nov 28 '18 at 2:00














          0












          0








          0







          console.log(body.req); this is wrong. It should be console.log(req.body);. This generally happens when the program exited but still, there is a function callback in the stack. This should fix your problem. If it didn't please share the console log if printed any






          share|improve this answer













          console.log(body.req); this is wrong. It should be console.log(req.body);. This generally happens when the program exited but still, there is a function callback in the stack. This should fix your problem. If it didn't please share the console log if printed any







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 9:11









          Aman BansalAman Bansal

          815




          815













          • addNewUser() was below the router.post() which is why it wasn't working

            – Shaz
            Nov 28 '18 at 2:00



















          • addNewUser() was below the router.post() which is why it wasn't working

            – Shaz
            Nov 28 '18 at 2:00

















          addNewUser() was below the router.post() which is why it wasn't working

          – Shaz
          Nov 28 '18 at 2:00





          addNewUser() was below the router.post() which is why it wasn't working

          – Shaz
          Nov 28 '18 at 2:00




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466723%2ferror-setting-header-after-it-is-sent-help-me-understand-why%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()