How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application

Posted on

Introduction

This article will show how to solve an unique error message. Actually this article has a relation with the previous article. That article exist in this link with the title of ‘How to Accept Data from a HTTP POST Request in a NodeJS Application’. Another article also with a relation with this article has the title of ‘How to Solve Error Message Cannot destructure property ‘identifier’ of ‘req.body’ as it is undefined in NodeJS Application’ in this link.

Basically, there are several steps before the error message appear. The first one is the execution of the NodeJS application as follows :

[admin@10 db]$ [admin@10 db]$ node app.js

After that, sending HTTP POST request in a simulation by running Postman application. The following output will present an image about the Postman application sending hTTP POST request :

How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application
How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application

But after sending data via HTTP POST request by clicking the Send button, the following output appear :

[admin@10 db]$ [admin@10 db]$ node app.js
Identifier : 0000000001
Full Name : undefined

The following is an actual content of the file with the name of ‘app.js’ :

const express = require("express");
const mysql = require("mysql");
const app = express();

app.use(express.urlencoded({ extended: true} ));
app.listen(3001);

app.post("/save",(req, res) => {
        try {
                const { identifier, name } = req.body;
                console.debug("Identifier : "+identifier);
                console.debug("Full Name : "+name);
        } catch(err) {
                console.error(err.message);
                res.status(500).send("Server Error !");
        }
});

Solution to Solve the Problem

 

Apparently, after trying to look for a workout of the problem, there is a theory and base assumption to solve the problem. Using several trial and error attempt, the problem rely on the usage of the name of the variable. In order to solve the problem, just change the parameter with the name of ‘name’ into another possible parameter name. For an example, just change the ‘name’ into ‘fullname’. So, the ‘app.js’ file after the change process, it will have the following content :

const express = require("express");
const mysql = require("mysql");
const app = express();

app.use(express.urlencoded({ extended: true} ));
app.listen(3001);

app.post("/save",(req, res) => {
        try {
                const { identifier, fullname } = req.body;
                console.debug("Identifier : "+identifier);
                console.debug("Full Name : "+fullname);
        } catch(err) {
                console.error(err.message);
                res.status(500).send("Server Error !");
        }
});

Soon after, do not forget to execute once more in the Postman application. Before sending the HTTP POST request, do not forget to change the parameter name from ‘name’ into ‘fullname’ as follows :

How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application
How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application

After changing the ‘name’ parameter into ‘fullname’ parameter, the sending process of HTTP POST request from Postman application to the listening service of running NodeJS application is a success. The success exist in the article as mentioned earlir. It exist in the article with the title of ‘How to Accept Data from a HTTP POST Request in a NodeJS Application’ in this link.

One thought on “How to Solve Error Message name undefined on sending HTTP POST Request in NodeJS Application

Leave a Reply