Home > Software design >  Cannot read properties of undefined 'res.status()', same undefined error for 'res.sen
Cannot read properties of undefined 'res.status()', same undefined error for 'res.sen

Time:03-30

In the code below, as shown in the image, I am getting the error for res.

Every function that I chain with res (like res.status().send(), res.json(), res.console.log() and more) is throwing the Cannot read property of undefined "chainedFunction" error.

And it is happening only with this launches.controller.js file,

I have another file called planets.controller.js and it works absolutely fine with the same code pattern and res.status() or any other chained function with res does not throw any error. I have also parsed the request with express.json(), so it's not the problem of the request not getting parsed too.

There is no problem with data, as you can see, data is being logged in the console (in green color). Then just why is it throwing this error?

Please anyone help, this is very important for me. I won't be able to move forward in the project without resolving this issue.

Thank You.

This image shows Launches.controller.js. enter image description here

This image shows Launches.router.jsenter image description here

This image shows code for app.jsenter image description here

CodePudding user response:

The problem is that you need to pass a function reference to the route, not the result of the function.

So instead of calling the getAllLaunches function,like you did:

launchesRouter.get('/launches', getAllLaunches())

Change it to the following code:

//pass the function reference; do not execute the function.
//express will execute it when necessary.
launchesRouter.get('/launches', getAllLaunches)
  • Related