0

我是 NodeJS 的新手,但我想学习一些新东西。我来自 .NET 花式依赖注入、控制反转、微服务闪亮世界,所以我尝试根据我以前的经验用 TypeScript 编写一些服务。我正在使用 express 和 express 路由器来创建一些 api。我在路由器中有一些处理 api 调用的方法,我想使用某种服务对象进行数据检索和操作。我使用构造函数注入将服务注入路由器,但如果我想使用我的服务,它会引发错误:

TypeError:无法读取未定义的属性“layoutService”

我知道这些方法是在没有上下文的情况下调用的,所以我添加.bind(this)了每个方法注册并且它有效,但我不知道这是否是最好的方法。

有没有人有更好的主意?

简化的 server.ts

import express, { Router } from "express";

// inversion of controll
import container from "./ioc";
import { TYPE } from "./constants";

import IMyService from "./abstract/IMyService";

// import routers
import MyRouter from "./api/MyRouter";

app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const router: Router = express.Router();
const myRouter: MyRouter = new MyRouter(container.get<IMyService>(TYPE.IMyService));

app.use("/", router);
app.use("/api/v1/layouts", layoutRouter.router);

我的路由器.ts

import IMyService from "./abstract/IMyService";
import { Router, Request, Response } from "express";
import { inject } from "inversify";
import { TYPE } from "../constants";

export default class MyRouter {
    public readonly router: Router;
    private readonly myService: IMyService;

    constructor(
        @inject(TYPE.IMyService) myService: IMyService
    ) {
        this.myService = myService;
        this.router = Router();
        this.routes();
    }

    public GetAll(req: Request, res: Response): void {
        this.myService.getAll()
            .then(data => {
                const status: number = res.statusCode;

                res.json({ status, data });
            })
            .catch(err => {
                const status: number = res.statusCode;

                res.json({ status, err });
            });
    }   

    public GetOne(req: Request, res: Response): void {
        const id: string = req.params.id;

        this.myService.getOne(new ObjectID(id))
            .then(data => {
                const status: number = res.statusCode;

                res.json({ status, data });
            })
            .catch(err => {
                const status: number = res.statusCode;

                res.json({ status, err });
            });
    }

    routes(): void {
        this.router
            .get("/", this.GetAll)
            .get("/:id", this.GetOne);
    }
}
4

1 回答 1

0

如果你用箭头语法(ES6)定义你的函数,它会自动将上下文“绑定”到它,你就不需要bind它们了。但这取决于您的用例(您可能需要绑定不同的上下文)

于 2018-06-29T20:33:09.993 回答