Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot destructure property 'name' of 'req.body' as it is undefined. #87

Open
Ajaydevsysart opened this issue Jan 15, 2021 · 17 comments

Comments

@Ajaydevsysart
Copy link

Ajaydevsysart commented Jan 15, 2021

anyone help me?

@swoorpious
Copy link

swoorpious commented Jan 17, 2021

yeah, I'm also having the same problem... I guess I will figure it out soon, and comment the solution later.

@enkeyz
Copy link

enkeyz commented Feb 1, 2021

You need to use express's body parser before adding routes to express. Like this:

const app = express();
app.use(express.json());
app.use(userRoutes);
app.use(taskRoute)

@kushwahasanket
Copy link

you can individually take those value like
var name = req.body.ele_name
or if u want in object
var n = {
name: req.body.ele_name,
password: req.body.ele_pwd
}

@realsubhajitpatra
Copy link

realsubhajitpatra commented May 31, 2022

you can preview your code again

@omkarpatne38
Copy link

app.use(express.json());
may be this is missing in your server file

@Ksj14-kumar
Copy link

Ksj14-kumar commented Jun 1, 2022 via email

@Hussain101
Copy link

You need to use express's body parser before adding routes to express. Like this:
const app = express();
app.use(express.json());

@swapnilbarve
Copy link

yes i have same prob

@dulshan-devops
Copy link

const app = express();
app.use(express.json());

why not working for me ?

// IMPORTS FROM PACKAGES
const express = require("express");
const mongoose = require("mongoose");

// IMPORTS FROM OTHER FILES
const authRouter = require("./routes/auth");

// INIT
const PORT = 3000;
const app = express();

const DB =
"uri";

// middleware
app.use(authRouter);
app.use(express.json());

// Connections
mongoose
.connect(DB)
.then(() => {
console.log("DB Connection Successful");
})
.catch((e) => {
console.log(e);
});

app.listen(PORT, "ip", () => {
console.log(server connected at port ${PORT});
});

@avnikumar
Copy link

This issue is because body data is not converted into JSON data. So use the below code in the same sequence.

app.use(express.json());
app.use(userRoutes);

It works for me.

@geldartz
Copy link

add this to ur main js.
app.use(express.json());

then dont use postman when you do the testing. use Thunder Client on VScode extention.

@Tenzin1000
Copy link

const app = express();

app.use(express.json()); // to accept JSON data
app.use("/api/user", userRoutes);

Check if you had added res at firs place instead of req:
async (req, res) => {
const { name, email, password, pic } = req.body;
}

@shoaib9670
Copy link

const app = express();
app.use(express.json());

add the above two lines it worked for me

@ThaiHoang-newbie
Copy link

You need to use express's body parser before adding routes to express. Like this: const app = express(); app.use(express.json());

It work

@Sumit-Kumar-0
Copy link

app.use(express.json()); may be this is missing in your server file

thanks bro i was searching friom an hour and got the answer !!! ;) silly mistake

@ahshobuj1
Copy link

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

use body-parser package and added those line top of the app file..

@supratimsd
Copy link

app.use(express.json()); may be this is missing in your server file

In spite of writing this piece of code same error are showing. what to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests