Node.js 扩展CRUD实现的基类时出错

Node.js 扩展CRUD实现的基类时出错,node.js,typescript,Node.js,Typescript,我正在尝试为node.js应用程序在typescript中实现一个基类。其思想是所有实体/对象都可以扩展基类以用于其CRUD 下面是基类: export class Base { Model: any; constructor(modelName: object) { this.Model = modelName; } public getAll(req: Request, res: Response) { console.log

我正在尝试为node.js应用程序在typescript中实现一个基类。其思想是所有实体/对象都可以扩展基类以用于其CRUD

下面是基类:

export class Base {
    Model: any;
    constructor(modelName: object) {
        this.Model = modelName;

    }
    public getAll(req: Request, res: Response) {
        console.log(this.Model);
        this.Model.find({}, (err: Error, result: any) => {
            if (err) {
                res
                    .status(HttpStatus.NOT_FOUND)
                    .send({
                        message: HttpStatus.getStatusText(HttpStatus.NOT_FOUND),
                        error: err
                    });
            }
            res
                .status(HttpStatus.OK)
                .send({
                    message: HttpStatus.getStatusText(HttpStatus.OK),
                    output: result
                });
        });
    }

    public get(params: object, res: Response) {
        this.Model.find(params, (err: Error, result: any) => {
            if (err) {
                res
                    .status(HttpStatus.NOT_FOUND)
                    .send({
                        message: HttpStatus.getStatusText(HttpStatus.NOT_FOUND),
                        error: err
                    });
            }
            res
                .status(HttpStatus.OK)
                .send({
                    message: HttpStatus.getStatusText(HttpStatus.OK),
                    output: result
                });
        });
    }

    public create(params: object, res: Response) {
        console.log(this.Model);
        this.Model.create(params, (err: Error, result: any) => {
            if (err) {
                res
                    .status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .send({
                        message: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR),
                        error: err
                    });
            }
            res
                .status(HttpStatus.CREATED)
                .send({
                    message: HttpStatus.getStatusText(HttpStatus.CREATED),
                    output: result
                });
        });
    }

    public update(params: object, res: Response) {
        this.Model.update(params, (err: Error, result: any) => {
            if (err) {
                res
                    .status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .send({
                        message: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR),
                        error: err
                    });
            }
            res
                .status(HttpStatus.OK)
                .send({
                    message: HttpStatus.getStatusText(HttpStatus.OK),
                    output: result
                });
        });
    }

    public delete(params: object, res: Response) {
        this.Model.delete(params, (err: Error, result: any) => {
            if (err) {
                res
                    .status(HttpStatus.NOT_FOUND)
                    .send({
                        message: HttpStatus.getStatusText(HttpStatus.NOT_FOUND),
                        error: err
                    });
            }
            res
                .status(HttpStatus.OK)
                .send({
                    message: HttpStatus.getStatusText(HttpStatus.OK),
                    output: result
                });
        });
    }

    public search(req: Request, res: Response) {

    }

    public filter(req: Request, res: Response) {

    }
}
儿童课程如下:

class AssetClass extends Base {
    constructor(model: object) {
        model = AssetClassModel;
        logger.debug("creating the assetClass constructor");
        super(model);
    }

    public get(req: Request, res: Response) {
        const params: object = {
            name: req.body.name
        };
        super.get(params, res);
    }

    public create(req: Request, res: Response) {
        logger.debug("creating the asset class");
        const params: object = {
            name: req.body.name,
            manufacturer: req.body.manufacturer,
            category: req.body.category
        };
        super.create(params, res);
    }

    public update(req: Request, res: Response) {
        const params: object = {
            name: req.body.name,
            manufacturer: req.body.manufacturer,
            category: req.body.category
        };
        super.update(params, res);
    }

    public delete(req: Request, res: Response) {
        const params: object = {
            name: req.body.name,
        };
        super.delete(params, res);
    }
}

export const assetClass: AssetClass = new AssetClass(AssetClassModel);
我在我的app.ts中定义了两条路线:create和getAll

app.post("/test", assetClass.create);
app.get("/test", assetClass.get);
当我测试应用程序时,出现以下错误:

[Node] TypeError: Cannot read property 'Model' of undefined
[Node]     at create (/home/hassaan/apollo-be/rest/dist/controllers/base.controller.js:49:26)
[Node]     at create (/home/hassaan/apollo-be/rest/dist/controllers/assetClass.controller.js:28:15)
[Node]     at Layer.handle [as handle_request] (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/layer.js:95:5)
[Node]     at next (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/route.js:137:13)
[Node]     at Route.dispatch (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/route.js:112:3)
[Node]     at Layer.handle [as handle_request] (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/layer.js:95:5)
[Node]     at /home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:281:22
[Node]     at Function.process_params (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:335:12)
[Node]     at next (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:275:10)
[Node]     at serveStatic (/home/hassaan/apollo-be/rest/node_modules/serve-static/index.js:75:16)
[Node]     at Layer.handle [as handle_request] (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/layer.js:95:5)
[Node]     at trim_prefix (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:317:13)
[Node]     at /home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:284:7
[Node]     at Function.process_params (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:335:12)
[Node]     at next (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:275:10)
[Node]     at app.use (/home/hassaan/apollo-be/rest/dist/app.js:86:5)
[Node]     at Layer.handle [as handle_request] (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/layer.js:95:5)
[Node]     at trim_prefix (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:317:13)
[Node]     at /home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:284:7
[Node]     at Function.process_params (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:335:12)
[Node]     at next (/home/hassaan/apollo-be/rest/node_modules/express/lib/router/index.js:275:10)
[Node]     at app.use (/home/hassaan/apollo-be/rest/dist/app.js:71:5)
从这个错误中可以看出,基类中的this.Model是通过assetClass类的未实例化对象调用的,即未定义的模型

但是,如果我在app.ts文件中记录assetClass对象,我可以看到它是一个有效的已定义实例


我做错了什么?

在Javascript中,这个
是谁,由调用者决定,当你把一个函数传递给某人时,你只传递了一个函数,没有它来自的实例的内存。这就是为什么当路由引擎调用
create
时,
未定义的
。要将实例绑定到函数,需要使用
bind
函数或箭头函数:

app.post("/test", assetClass.create.bind(assetClass));
app.get("/test", assetClass.get.bind(assetClass));

// Or
app.post("/test", (req, res) => assetClass.create(req, res));
app.get("/test", (req, res) => assetClass.get(req, res));

在Javascript中,谁
这个
是,由调用方决定,当您将函数传递给某人时,您只传递了函数,而没有它来自的实例的内存。这就是为什么当路由引擎调用
create
时,
未定义的
。要将实例绑定到函数,需要使用
bind
函数或箭头函数:

app.post("/test", assetClass.create.bind(assetClass));
app.get("/test", assetClass.get.bind(assetClass));

// Or
app.post("/test", (req, res) => assetClass.create(req, res));
app.get("/test", (req, res) => assetClass.get(req, res));

非常感谢@Titan。你解释得很好。接受你的回答。非常感谢@Titan。你解释得很好。接受你的回答。