Skip to content
Advertisement

Node.js throws TypeError: Cannot destructure property ‘firstName’ of ‘req.body’ as it is undefined, even though it works for an other post request

So I know there are tons of similar questions out there, and I’ve read most of them in the past few days. However I didn’t find any solution to my problem. The app is about users can post memories(cards) etc… Point is, when I create a new card with POST request, there is no problem, but when I want to sign up a user then all hell breaks loose and throws this error:

(node:2732) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property ‘firstName’ of ‘req.body’ as it is undefined. at signup (file:///E:/projects/personal/memories-app/backend/controllers/user.controller.js:39:13) at Layer.handle [as handle_request] (E:projectspersonalmemories-appbackendnode_modulesexpresslibrouterlayer.js:95:5) at next (E:projectspersonalmemories-appbackendnode_modulesexpresslibrouterroute.js:137:13)

I don’t know that could be the problem, because other functions work so dunno really. Here are the codes

server.js

JavaScript

user.model.js

JavaScript

the sign up method from user.controller.js

JavaScript

and just to see the createPost method (which works) from post.controller.js

JavaScript

And there is no problem with the front-end because when I simply console.log the req, I can see the body, but if I were to clg the req.body, then it is undefined. I’ve tried it with postman also, but no luck.

I would appreciate any insight on this! Thanks in advance!

Advertisement

Answer

You need to swap the order of res and req in the signup function, replace:

JavaScript

by:

JavaScript
Advertisement