TypeError: Cannot destructure property 'name' of 'req.body' as it is undefined.
Opened this issue ยท 17 comments
anyone help me?
yeah, I'm also having the same problem... I guess I will figure it out soon, and comment the solution later.
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)
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
}
you can preview your code again
app.use(express.json());
may be this is missing in your server file
You need to use express's body parser before adding routes to express. Like this:
const app = express();
app.use(express.json());
yes i have same prob
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}
);
});
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.
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.
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;
}
const app = express();
app.use(express.json());
add the above two lines it worked for me
You need to use express's body parser before adding routes to express. Like this: const app = express(); app.use(express.json());
It work
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
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..
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?