1

对节点和打字稿有点新,所以需要一点帮助。使用 node/express/postres 作为后端,使用https://github.com/typeorm/typeorm作为 orm,它具有打开连接的功能,如下所示:

export declare function createConnection(): Promise<Connection>;

所以当我想在我的路线中使用它时,它看起来像这样:

    this.app.get("/test", function (req, res) {

        createConnection()
            .then(conn => {
                console.log("Start connection...");
                let repo = conn.getRepository(entForum);

                let obj = repo.find();
                conn.close().catch(err => { });
                return obj;
            })
            .then(data => {
                res.send(data);
            });

        console.log("Done");
    });

控制台输出是:

Done
GET /test/ 200 51.053 ms - 1133
Start connection...

问题是我收到“发送后无法设置标题。”。看起来响应是在 console.log("DONE"); 之后发送的 然后再承诺一次。我在这里做错了什么?

4

1 回答 1

0

Ok, found out what was wrong. Turns out the problem was this piece of code after the promise - console.log("Done");

If i remove that, everything works ok.

于 2017-01-16T18:53:24.783 回答