I'm trying to structure my Express.js code however I'm running into a problem trying to use my class method from the controller.
Heres what it looks like:
product.service.ts
export class ProductService {
constructor(private readonly model: Model<ProductType>) {}
public getAllProducts = () => {
return this.model.find({});
};
}
product.controller.ts
export class ProductController {
constructor(private readonly service: ProductService) {}
public getProducts = async (req: Request, res: Response) => {
const response: ProductType[] = await this.service.getAllProducts();
res.send(response);
};
}
products.router.ts
import express, { Router } from 'express';
import { ProductController } from '../controllers/product.controller';
const router: Router = express.Router();
router.get('/', ProductController.getProducts);
The problem is in my products.router.ts, it keeps saying
Property 'getProducts' does not exist on type 'typeof ProductController'.ts(2339)
Could anyone advise me what I'm doing wrong?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment