扩展CRUD实现的基类时出错

时间:2018-06-12 11:10:33

标签: 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(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)

从我从这个错误中收集的内容中,通过assetClass类的未实例化对象调用基类中的this.Model,即未定义模型

但是如果我在app.ts文件中调用asset.log资产块对象,我可以看到它是一个有效的定义实例。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

在javacript中,this是由调用者决定的,当你向某人传递一个函数时,你只传递函数而没有来自它的实例的内存。这就是路由引擎调用thisundefinedcreate的原因。要bind函数的实例,您需要使用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));