2017-04-15 33 views
0

請你能幫我解決我的路由問題嗎?我試圖在Node.js(express)中使用typescript和存儲庫模式來創建REST API。快速 - REST API - 存儲庫實例在路由期間丟失

我有兩個泛型類BaseRepositoryBaseController一起處理基本的CRUD交易。其他域控制器是從這些派生出來的。

有我的代碼:

productRouter.ts用來處理路線:

import { Router } from 'express'; 
 

 
import { ProductController } from '../controllers/ProductController'; 
 

 
class ProductRouter { 
 
    private _router: Router; 
 
    private _controller: ProductController; 
 

 
    constructor() { 
 
     this._router = Router(); 
 
     this._controller = new ProductController; 
 
    } 
 

 
    get routes(): Router { 
 
     this._router.get('/product', this._controller.getAll); 
 
     this._router.post('/product', this._controller.create); 
 
     this._router.get('/product/:id', this._controller.getById); 
 
     this._router.put('/product/:id', this._controller.update); 
 
     this._router.delete('/product/:id', this._controller.delete); 
 
     return this._router; 
 
    } 
 
}

productController.ts用於初始化的BaseRepository其從BaseController的。

import { BaseController } from '../common/BaseController'; 
 
import { BaseRepository, IRepository } from '../common/BaseRepository'; 
 

 
import { Product, IProduct } from '../models/Product'; 
 

 
export class ProductController extends BaseController<IProduct> { 
 

 
    constructor() { 
 
     const productRepository = new BaseRepository<IProduct>(Product); 
 
     super(productRepository); 
 
    } 
 
}

BaseController.ts

export class BaseController<T> implements IController { 
 
    private _repository: IRepository<T>; 
 

 
    constructor(repository: IRepository<T>) { 
 
     this._repository = repository; 
 
    } 
 
    getAll(req: Request, res: Response, next: NextFunction): void { 
 
     this._repository.getAll((err, result) => { 
 
      if (err) 
 
       res.send(err); 
 

 
      res.json(result); 
 
     }); 
 
    }

我每次導航到合適的路徑,我得到以下錯誤:

TypeError: Cannot read property '_repository' of undefined 
    at BaseController.getAll (C:\Users\vrbat\Documents\Projects\router-test\src\api\common\BaseController.ts:19:13) 
    enter code here 

我不知道爲什麼,因爲_repository屬性是在productController.ts中進行的。

+1

哪一行是你的BaseController.ts中的第19行。根據錯誤消息,我們需要看到第19行。 – Eray

+0

第19行是調用this._repository.getAll函數的地方。 – jezikk

回答

0

經過一番挖掘,我發現問題出在屬性的錯誤範圍界定上。

下面的代碼將修復它:

productRouter.ts

get routes(): Router { 
 
     this._router.get('/product', this._controller.getAll()); 
 
     this._router.post('/product', this._controller.create()); 
 
     this._router.get('/product/:id', this._controller.getById()); 
 
     this._router.put('/product/:id', this._controller.update()); 
 
     this._router.delete('/product/:id', this._controller.delete()); 
 
     return this._router; 
 
    }

BaseController.ts

export class BaseController<T> implements IController { 
 
    private _repository: IRepository<T>; 
 

 
    constructor(repository: IRepository<T>) { 
 
     this._repository = repository; 
 
    } 
 
    getAll() { 
 
     const repository = this._repository; 
 
     return (req: Request, res: Response, next: NextFunction) => { 
 
      repository.getAll((err, result) => { 
 
       if (err) 
 
        res.send(err); 
 

 
       res.json(result); 
 
      }); 
 
     }; 
 
    }