-1

i want to access to class function when wxtends that class in typescript .

this is my class BaseController :

   "use strict";

import { validationResult } from "express-validator";
import applyBind from 'auto-bind';

export class ValidationResult {
    haveError: boolean;
    errorMessage: string[];

    constructor(haveError: boolean, errorMessage: string[]) { }
}

export default class BaseCotnroller {

    constructor() {
        applyBind.bind(this);
    }

   static async ValidationAction(req, res): Promise<ValidationResult> {
        const result = await validationResult(req);
        if (!result.isEmpty()) {
            let errors = result.array();
            let message = [];
            errors.forEach((element) => {
                message.push(element.msg);
            });
            return new ValidationResult(true, message);
        }
        return new ValidationResult(false, null);
    }

}

and then extends the BaseController :

   import BaseCotnroller from './BaseController';


export default class PermissionController extends BaseCotnroller {

    constructor() {
        super();
    }

    /*** Create Permission ****/
    static async CreatePermission(req, res, next) {
            let validationData = await this.ValidationAction(req, res); 
            if (!validationData.haveError) {
                PermissionRepository.CreatePermission(req)
                    .then(() => {
                        this.Ok(res);
                    })
                    .catch((error) => {
                        return this.BadRerquest(res, error);
                    });
            } else {
                return this.BadRerquest(res, validationData.errorMessage);
            }   
    }


}

but it show me this error :

error TypeError: Cannot read property 'ValidationAction' of undefined at F:\Projects\Nodejs\Travel Budy\src\http\controller\PermissionController.ts:20:45 at Generator.next () at F:\Projects\Nodejs\Travel Budy\src\http\controller\PermissionController.ts:8:71 at new Promise () at __awaiter (F:\Projects\Nodejs\Travel Budy\src\http\controller\PermissionController.ts:4:12) at CreatePermission (F:\Projects\Nodejs\Travel Budy\src\http\controller\PermissionController.ts:24:16) at Layer.handle [as handle_request] (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\layer.js:95:5) at next (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\layer.js:95:5) at F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\index.js:281:22 at Function.process_params (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\index.js:335:12) at next (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\index.js:275:10) at Function.handle (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\index.js:174:3) at router (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (F:\Projects\Nodejs\Travel Budy\node_modules\express\lib\router\layer.js:95:5)

now whats the problem ? how can i solve this problem ?

StPaulis
  • 2,844
  • 1
  • 14
  • 24
kianoush dortaj
  • 411
  • 7
  • 24
  • How are you calling that static `CreatePermission` method? – Jared Smith Oct 02 '20 at 11:22
  • i calling that by this : `import permission from './../../http/controller/PermissionController'; const router = express.Router(); router.post('/create', permission.CreatePermission)` – kianoush dortaj Oct 02 '20 at 11:24
  • Does this answer your question? [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Jared Smith Oct 02 '20 at 12:00
  • @JaredSmith this is not worked for me man – kianoush dortaj Oct 02 '20 at 12:11
  • That's just the auto-generated comment when somebody links your question as a duplicate of an existing one. You can't pass a method as a callback without binding it first. – Jared Smith Oct 02 '20 at 13:40

1 Answers1

1

Try to bind CreatePermission method to permission class when registering the route handler:

router.post('/create', permission.CreatePermission.bind(permission));

Alternatively, you can change static methods to regular ones, create a PermissionController instance and use it for route handling:

import PermissionController from './../../http/controller/PermissionController';
const permission = new PermissionController()
const router = express.Router();  
router.post('/create', permission.CreatePermission)
antonku
  • 7,377
  • 2
  • 15
  • 21
  • it worked but i think this is not good way . i using this `constructor() { applyBind.bind(this); }` but its not worked. why ? – kianoush dortaj Oct 02 '20 at 11:58
  • `applyBind.bind(this);` does not work for `static` methods because `this` inside the `constructor` refers to the concrete instance while `this` inside static methods refers to the class itself. – antonku Oct 02 '20 at 12:12
  • if i can remove the static , it not show me that function . why ? – kianoush dortaj Oct 02 '20 at 12:14
  • Make sure that you create an instance and use it when registering the route handler. I have updated the answer respectively. – antonku Oct 02 '20 at 12:21