我正在嘗試從我創建的其余 API 訪問資料,但是路由未顯示任何 JSON 資料。我有用于初始化和配置 API 的服務器、控制器和資料控制器類。服務器與兩個控制器一起正確加載,但是當我轉到端點時,它只顯示默認的 React 應用程式頁面。
服務器負載控制器方法:
public loadControllers(controllers: Array<Controller>): void {
controllers.forEach(controller => {
console.log(controller, controller.path)
this.app.use(controller.path, controller.setRoutes());
});
};
控制器設定路由方法:
public setRoutes = (): Router => {
for (const route of this.routes) {
for (const mw of route.localMiddleware) {
this.router.use(route.path, mw)
};
switch (route.method) {
case Methods.GET:
this.router.get(route.path, route.handler);
break;
case Methods.POST:
this.router.post(route.path, route.handler);
break;
case Methods.PUT:
this.router.put(route.path, route.handler);
break;
case Methods.DELETE:
this.router.delete(route.path, route.handler);
break;
default:
console.log('not a valid method')
break;
};
};
return this.router;
}
DataController 示例路由和處理程式:
routes = [
{
path: '/locations',
method: Methods.GET,
handler: this.handleGetLocations,
localMiddleware: [],
},
]
async handleGetLocations (req: Request, res: Response, next: NextFunction, id?: number) {
try{
this.dbConn.initConnectionPool()
.then((pool) => {
pool.query(`query here`)
})
.then((data) => {
res.send(data);
})
}
catch(err){
console.log(err);
}
}
當我控制臺記錄路線或任何功能時,它們都會正確顯示。
uj5u.com熱心網友回復:
首先,您在this處理方面存在問題。當你做這樣的事情時:
this.router.get(route.path, route.handler);
并router.handler指向一個函式,例如你的函式,handleGetLocations()它期望使用this并期望它是你的物件,值this不會有正確的值。你要么需要.bind() this你的函式,要么你需要正確使用arrow函式來保留this.
當 的值this錯誤時,this.dbConn.initConnectionPool()您的 handleGetLocations()中的function will fail becausethis` 就不是正確的值。
您沒有在問題的代碼中顯示足夠的整體背景關系,以便我們理解this應該指向的內容以及如何正確宣告或構造事物來解決問題。
有可能,您可以更改此代碼:
routes = [
{
path: '/locations',
method: Methods.GET,
handler: this.handleGetLocations,
localMiddleware: [],
},
]
對此:
routes = [
{
path: '/locations',
method: Methods.GET,
handler: this.handleGetLocations.bind(this),
localMiddleware: [],
},
]
但是,只有this在我們沒有足夠的代碼背景關系來真正知道的陣列宣告中有正確的值時,這才有效。您的代碼中還有其他問題,例如handleGetLocations(). 如果this.dbConn.initConnectionPool()拒絕,則您沒有處理程式。你要么需要await它,這樣你try/catch才能抓住拒絕,要么你需要一個.catch()之后.then()才能抓住拒絕。
僅供參考,在另一個框架之上添加您自己的框架在這??里似乎不必要地復雜。你用你自己的系統掩蓋了 Express 中非常簡單的路由宣告,這引入了新問題,并沒有真正使任何事情變得更清晰,而且也是一個其他人都不熟悉的系統。
哦,還有一件事。這段代碼:
async handleGetLocations(req: Request, res: Response, next: NextFunction, id ? : number) {
try {
await this.dbConn.initConnectionPool()
.then((pool) => {
pool.query(`query here`)
})
.then((data) => {
res.send(data);
})
} catch (err) {
console.log(err);
}
}
需要修復幾件事。如上所述,您必須修復this傳遞給此函式的方式。然后,您必須修復錯誤處理以從此處的任何一個異步呼叫中實際捕獲被拒絕的承諾。然后,您必須實際捕獲結果,pool.query()以便您可以用它做一些事情。你忽略了它。然后,如果 Promise 被拒絕,您必須實際發送錯誤回應。
async handleGetLocations(req: Request, res: Response, next: NextFunction, id ? : number) {
try {
const pool = await this.dbConn.initConnectionPool();
const data = await pool.query(`query here`);
res.send(data);
} catch (err) {
console.log(err);
res.sendStatus(500);
}
}
Whether there are other problems in your framework on top of a framework is impossible for us to tell with this amount of code. Stepping through a simple test app with one GET route in it should be able to determine if you end up configuring the proper Express route and whether that route handler ever gets called. Since we don't have a reproducible, runnable set of code here that's not something we can do. As I said above, I'm not a fan of putting your own framework on top of an existing framework because it just seems like an added level of complexity without significant benefit. And, you have to properly debug (since it doesn't yet work) and maintain your framework now too.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444500.html
上一篇:Haskell:使用具有特定背景關系的函式對異構串列進行同質化
下一篇:剩余引數和引數化型別
