在下面的代碼中,如圖所示,我收到了res.
Every我鏈接的函式res(如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
uj5u.com熱心網友回復:
問題是您需要將函式參考傳遞給路由,而不是函式的結果。
因此,不要像您那樣呼叫 getAllLaunches 函式:
launchesRouter.get('/launches', getAllLaunches())
將其更改為以下代碼:
//pass the function reference; do not execute the function.
//express will execute it when necessary.
launchesRouter.get('/launches', getAllLaunches)
uj5u.com熱心網友回復:
在app.js.
更改這些行:
app.use(planetsRouter);
app.use(launchesRouter);
到
app.use('/', planetsRouter);
app.use('/', launchesRouter);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452179.html
