繁体   English   中英

在 nodejs 打字稿中扩展类时未定义的函数

[英]Undefined function when extends class in nodejs typescript

我想在打字稿中 wxtend 该类时访问类函数。

这是我的类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);
    }

}

然后扩展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);
            }   
    }


}

但它向我展示了这个错误:

错误类型错误:无法读取 F:\\Projects\\Nodejs\\Travel Budy\\src\\http\\controller\\PermissionController.ts:20:45 at Generator.next () at F:\\Projects\\Nodejs\\Travel 处未定义的属性“ValidationAction” 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) 在 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) 在下一个 (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\route.js:137:13) 在 Route.dispatch (F:\\Projects\\Nodejs\\ Travel Budy\\node_modules\\express\\lib\\router\\route.js:112:3) 在 Layer.handle [作为 handle_request] (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\layer.js: 95:5) 在 F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\index.js:281:22 在 Function.process_params (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\index.js:335:12) 在下一个 (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\index.js:335:12) js:275:10) 在 Function.handle (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\index.js:174:3) 在路由器 (F:\\Projects\\Nodejs\\Travel Budy\\ node_modules\\express\\lib\\router\\index.js:47:12) 在 Layer.handle [作为 handle_request] (F:\\Projects\\Nodejs\\Travel Budy\\node_modules\\express\\lib\\router\\layer.js:95:5 )

现在有什么问题吗? 我怎么解决这个问题 ?

注册路由处理程序时尝试将CreatePermission方法绑定到permission类:

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

或者,您可以将static方法更改为常规方法,创建一个 PermissionController 实例并将其用于路由处理:

import PermissionController from './../../http/controller/PermissionController';
const permission = new PermissionController()
const router = express.Router();  
router.post('/create', permission.CreatePermission)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM