我正在尝试学习TypeScript,但是有些想法使我无法正确理解...
我遇到此错误:
TypeError: Cannot set property 'test' of undefined
at testResponse (/home/mcfly/bimbim/geoloc_API/dist/routes/geoRouter.js:11:19)
而且我不知道如何解决。
import {
Router, Request, Response,
} from 'express';
export class GeoRouter {
router : Router
test : string
constructor() {
this.router = Router(); // the error point here... Why ?
// ^-------------------|
this.init();
}
public testResponse(_req: Request, _res: Response) {
this.test = _req.params.addr;
_res.send(this.test);
}
init() {
this.router.get('/:addr', this.testResponse);
}
}
const geoRoutes = new GeoRouter();
geoRoutes.init();
export default geoRoutes.router;
有人有主意吗?
答案 0 :(得分:0)
您需要在上指定req
参数:
this.router.get(url, req, res);