Introduction
This article will show how to solve an error message. The error message appear upon executing a NodeJS application. The NodeJS application is performing a process for retrieving data from a HTTP POST request. Later on, the NodeJS application will process that request by storing it into MySQL Database Server. Actually, the problem is quite critical. But the solution is very simple. Furthermore, this article has a relation with the previous one. It is an article with the title of ‘How to Connect to MySQL Database Server from a NodeJS Application’ in this link. The following is the actual error message :
[admin@10 db]$ node app.js Cannot destructure property 'identifier' of 'req.body' as it is undefined. ^C [admin@10 db]$
Actually, the above command for executing NodeJS application is just starting to listen in port 3001 for incoming HTTP POST request. After that, running Postman to generate HTTP POST request with the URL of ‘localhost:3001/save’. Read an article with the title of ‘How to Access Running NodeJS Service in Linux CentOS 8 virtual server’ in this link to learn how to access NodeJS service exist in guest machine or virtual server from a host machine. The following is the image of generating HTTP POST request from the Postman application :
The actual content of the script with the name of ‘app.js’ exist as follows :
const express = require("express"); const mysql = require("mysql"); const app = express(); app.listen(3001); const con = mysql.createConnection({ host: "10.0.2.2", user : "db_user", password : "password", database : "db_master" }); app.post("/save",(req, res) => { try { const { identifier, fullname } = req.body; console.debug("Identifier : "+identifier); console.debug("Full Name : "+fullname); con.connect(function(err){ if(err) console.debug(err); else console.log("Connected !"); }); } catch(err) { console.error(err.message); res.status(500).send("Server Error !"); } });
Solution to Solve the Error Message
So, the main goal is to solve the problem for handling the error message. It is to retrieve data from a HTTP POST request and then parse it into an associated variable respectively. Later on, print it to the console using ‘debug’ function from console. The solution is very simple. Just add the following line before defining the function for retriving incoming HTTP POST request :
app.use(express.urlencoded({ extended: true} ));
Since in the image above, the sending process of the HTTP POST request is using the type of x-www-form-urlencoded, the above line of code is a necessary. It will inform to NodeJS application to use an urlencoded type. It is important that the NodeJS application can further decode after receiving the HTTP POST request. Further explanation about it exist in this link. Another one exist in the default official website of Postman in this link. So, overall the content of the app.js file will be exist as follows :
const express = require("express"); const mysql = require("mysql"); const app = express(); app.use(express.urlencoded({ extended: true} )); app.listen(3001); const con = mysql.createConnection({ host: "10.0.2.2", user : "db_user", password : "password", database : "db_master" }); app.post("/save",(req, res) => { try { const { identifier, fullname } = req.body; console.debug("Identifier : "+identifier); console.debug("Full Name : "+fullname); con.connect(function(err){ if(err) console.debug(err); else console.log("Connected !"); }); } catch(err) { console.error(err.message); res.status(500).send("Server Error !"); } });
After adding one line as in the above script display, just run it once more. The NodeJS application and also the process for generating HTTP POST request in Postman application. If there is no further error appear, the following output will exist :
[admin@10 db]$ node app.js Identifier : 0000000001 Full Name : Blake Morrison Connected ! ^C [admin@10 db]$
Thank you my error is solved now i am stuck here for 1 hour.