Saturday 27 May 2017

'This' is undefined in typescript even using arrow func in a promise

When I receive the 'catch' callback, 'this' is undefined, even using arrow funcitions. Any ideas?

private all(req: Request, res: Response): void {
    EntityRepository.getRepositoty(req.params.etName).then(repo => {

        ...

    }).catch((err) => {
        this.handleError(res, err); // here I get undefined.
    });
}

how the all func is called.

It's called based on a Express route.

constructor() {
    // Connect to database.
    DataAccess.connect();

    // Starts configuring routes for api
    this.router = Router();

    // Bad request due to absence of entity type.
    this.router.get("/", (req, res) => {
        res.statusCode = 400;
        res.statusMessage = SysMsgs.error.noEntityTypeSpecified.message;
        res.send();
    });

    this.router.get(this.routeBase, this.all);
    this.router.get(this.routeBase + "/:id", this.findOne);
}



via Leandro

No comments:

Post a Comment