How to Resolve form-data is getting undefined in Node/Express API ?
Last Updated :
28 Apr, 2025
Working with forms and handling data submissions is a basic requirement in web development. When using Node and Express for building APIs, you might get an error that the FormData object is undefined.
In this article, we'll explore how to resolve form data getting undefined in a Node/Express API.
The FormData object is a part of the Web API and is commonly used to construct a set of key/value pairs representing form fields and their values. It is often used in the frontend to send form data to a server, especially when dealing with file uploads.
There may be several reasons for this issue:
- Missing Middleware
- Incorrect Content-Type Header
- Body Parsing Configuration
We will discuss the following approaches to resolve this error
Approach 1: Install and Use body-parser Middleware
If you encounter issues parsing incoming POST request data, one effective solution is to utilize the body-parser middleware in your Node/Express application. This middleware facilitates the parsing of request bodies, enabling you to extract data from incoming requests.
Run the following command to install the body parser.
npm install body-parser
Basic syntax in ExpressJS application.
JavaScript
// index.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json()); // for JSON data
app.use(bodyParser.urlencoded({ extended: true })); // for URL-encoded data
app.post("/parse-data", (req, res) => {
// Access parsed data using req.body.
console.log(req.body);
res.send("Data parsed successfully!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
Approach 2: Submit Data Using Raw JSON Format in Postman
If the issue is specific to Postman, consider submitting data using raw JSON format instead of form data. Postman allows you to send requests with various data formats, and opting for raw JSON can help ensure the correct interpretation of your data on the server side.
Steps:
- When making a POST request in Postman, select the raw JSON option in the request body.
- Input your data in JSON format.

Approach 3: Verify and Use the Correct Middleware
Incorrectly choosing or configuring middleware to parse incoming request data can lead to parsing failures. Ensure that you are using the appropriate middleware for your specific use case, whether it's for JSON or URL-encoded data.
Steps:
- Confirm the use of body-parser middleware.
- Configure it according to the data format you expect (JSON or URL-encoded).
JavaScript
// index.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json()); // for JSON data
app.use(bodyParser.urlencoded({ extended: true })); // for URL-encoded data
app.post("/correct-middleware", (req, res) => {
// Access parsed data using req.body.
console.log(req.body);
res.send("Data parsed with correct middleware!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
The Content-Type header in incoming requests specifies the type of data being sent. An incorrect Content-Type header can lead to parsing errors. Ensure that the header is set to either application/json or application/x-www-form-urlencoded based on the type of data being transmitted.
Steps:
- Inspect the Content-Type header in incoming requests.
- Adjust it accordingly.
JavaScript
// index.js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json()); // for JSON data
app.use(bodyParser.urlencoded({ extended: true })); // for URL-encoded data
app.post("/check-content-type", (req, res) => {
// Access parsed data using req.body.
console.log(req.body);
res.send("Content-Type header checked successfully!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
Similar Reads
How to check for undefined property in EJS for Node.js ? Handling undefined attributes is essential when working with EJS templates in a NodeJS application to guarantee a seamless and error-free user experience. In this post, we'll examine one of the most basic methods for determining whether a variable is defined or not, which involves utilizing a simple
3 min read
How to check for undefined property in EJS for Express JS ? This article explores various techniques to handle undefined properties within EJS templates used in ExpressJS applications. It guides you on how to gracefully manage scenarios where data passed to the template might lack specific properties, preventing errors and ensuring a smooth user experience.
3 min read
How to Validate Data using express-validator Module in Node.js ? Validation in node.js can be easily done by using the express-validator module. This module is popular for data validation. There are other modules available in market like hapi/joi, etc but express-validator is widely used and popular among them.Steps to install express-validator module:Â Â You can
3 min read
How to Send Response From Server to Client using Node.js and Express.js ? In web development, sending responses from the server to the client is a fundamental aspect of building interactive and dynamic applications. Express.js, a popular framework for Node.js, simplifies this process, making it easy to send various types of responses such as HTML, JSON, files, and more. T
4 min read
How to Get Data from MongoDB using Node.js? One can create a simple Node.js application that allows us to get data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. Also, we use the EJS for our front end to render the simple HTML form and a table to show the data. Prerequisi
6 min read