POST Request returns 'undefined' (But NO errors in logs)

Okay, so when I send a POST request to /getenv, I am supposed to get the value of an env variable. The POST request works fine and there are no logs in either places (Glitch Logs and Chrome DevTools), but the problem is that the POST request returns undefined. I tried console.loging and I got the variable values correctly. Here’s how my code looks:


var to = process.env.TO;
var from = process.env.FROM;
var title = process.env.TITLE;
var message = process.env.MESSAGE;'/getenv', function(request, response) {
  var req =;
  if (request.body && {
    const getto = to;
    response.send({ name: getto });
  } else {
    response.json({ error: "no body included" });


var some = "Dear, ";
const data = { name: some };
fetch("/getenv", {
  method: "POST",
  body: JSON.stringify(data),
  headers: { "Content-Type": "application/json" }
  .then(response => console.log( response))
  .then(data => {
    // get the response from the server POST request
    // var dataa = JSON.stringify(data)
    // console.log(;
    document.getElementById("to").innerText = data;

In case you’re wondering, I have already installed body-parser :grinning:! But someone please help!!!

If using express 4.16.0 or later, instead of bodyparser you could do …


Then use instead of

No, it’s still the same :frowning:

Are you using the middleware before defining the handler? Order does matter for middleware.

FYI on the client.js it won’t show much until you put in a stream handler.

I’ve removed the body-parser code which was

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

and added app.use(express.json()); at the top of my server.js (after requiring express).

Should I have not removed the body-parser code?

And what do you mean???

Sounds correct, what does console.log( show?

Sorry I was jumping ahead a bit too much, better to focus on one part at a time :slight_smile:

1 Like

It shows undefined although returns Dear, which was our request.

Looks like you have bodyParser working after all :slight_smile:

But I removed body-parser as you said. I just changed to!

Weird! I just spun up a small demo in Glitch and got the same as you did … different to running on my local machine.

For anyone watching this, request.body should look like {"name":"something"}, @khalby786 is correct with I think my little test wasn’t refreshing lol

I created some confusion by assuming help was wanted with the server side body params. After a bit of messaging each other, I realised that the issue was with the client side fetch results.

Here is the fix for the fetch response in client.js

  .then(response => response.json())
  .then(data => {
    document.getElementById("to").innerText =;

Also note that fetch is not supported in Internet Explorer, here is a polyfill for fetch

Good luck @khalby786 !

1 Like

I can’t thank you enough, @mishavee! :+1: :smile: :clap:

1 Like