2017-04-15 2 views
0

Bitte können Sie mir mit meinem Routing-Problem helfen? Ich versuche, eine REST-API mithilfe von Typescript und Repository-Muster in Node.js (Express) zu machen.Express - REST API - Repository-Instanz ist während des Routings verloren

Ich habe zwei generische Klassen BaseRepository und Base, die zusammen die grundlegenden CRUD Geschäfte abwickeln. Andere Domänencontroller werden von diesen abgeleitet.

Es ist mein Code:

productRouter.ts verwendet Routen zu handhaben:

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 verwendet, um die BaseRepository init und seine aus dem Base abgeleitet.

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); 
 
     }); 
 
    }

Jedes Mal, wenn ich auf geeignete Route navigieren ich folgende Fehlermeldung erhalten:

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 

Ich weiß nicht warum, weil die Eigenschaft _repository in productController.ts initiiert wird.

+1

Welche Zeile ist die 19. Zeile in Ihrem BaseController.ts. Laut Fehlermeldung müssen wir die 19. Zeile sehen. – Eray

+0

Die 19. Zeile ist ein Ort, an dem die Funktion this._repository.getAll aufgerufen wird. – jezikk

Antwort

0

Nach einigem Graben habe ich herausgefunden, dass das Problem im falschen Bereich der _repository Eigenschaft liegt.

Der folgende Code wird das Problem beheben:

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); 
 
      }); 
 
     }; 
 
    }

Verwandte Themen