无法读取未定义 'res.status()' 的属性,'res.send()' 和 'res.json()' 的相同未定义错误。请任何人

Cannot read properties of undefined 'res.status()', same undefined error for 'res.send()' and 'res.json()'. Please anyone

在下面的代码中,如图所示,我收到 res 的错误。

我与 res 链接的

Every 函数(如 res.status().send()res.json()res.console.log() 等)正在抛出 Cannot read property of undefined "chainedFunction"错误。

它只发生在这个 launches.controller.js 文件中,

我有另一个名为 planets.controller.js 的文件,它在相同的代码模式下工作得非常好,res.status() 或任何其他带有 res 的链接函数不会抛出任何错误。 我也用express.json()解析了请求,所以这不是请求没有被解析的问题。

数据没有问题,如您所见,数据正在控制台中记录(绿色)。那为什么会抛出这个错误呢?

请任何人帮助,这对我来说非常重要。如果不解决这个问题,我将无法推进项目。

谢谢。

此图像显示 Launches.controller.js

这张图片显示 Launches.router.js

此图显示 app.js

的代码

问题是您需要将函数引用传递给路由,而不是函数的结果。

所以不要像您那样调用 getAllLaunches 函数:

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

改为以下代码:

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