bradtraversy / node_passport_login

Node.js login, registration and access control using Express and Passport
1.73k stars 1.3k forks source link

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

Open Ajaydevsysart opened 3 years ago

Ajaydevsysart commented 3 years ago

anyone help me?

swoorpious commented 3 years ago

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

enkeyz commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

you can preview your code again

omkarpatne38 commented 2 years ago

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

Ksj14-kumar commented 2 years ago

You passed the username in body data from client side.

On Wed, Jun 1, 2022, 15:05 omkarpatne38 @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/bradtraversy/node_passport_login/issues/87#issuecomment-1143364880, or unsubscribe https://github.com/notifications/unsubscribe-auth/APZCPMZIOIFKC6Y73GUDMG3VM4VFRANCNFSM4WD2JTPQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Hussain101 commented 1 year ago

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

swapnilbarve commented 1 year ago

yes i have same prob

dulshan-devops commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

add the above two lines it worked for me

ThaiHoang-newbie commented 8 months ago

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 commented 6 months ago

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 commented 2 months ago

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 commented 1 month ago

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?