jeffrey chan jeffrey chan - 6 months ago 10
Node.js Question

private Field in constructor won't be null, but in method will be null, typescript

here is my source code

when i call

api/cateogry/:id
something wrong with ControllerBase.ts
in the
findById
-method,
this._service
is null. but in the constructor,
this._service
is not null. I can tell what happen, and I don't know how to fix it.
please help me!

Answer

Typescript's this handling is sometimes ignored when calling functions directly rather than executing them on the class object. Instead of passing

this._controller.findById` 

to your router try passing

this._controller.findById.bind(this)