我如何访问Koa中的db查询结果?

时间:2016-04-05 02:04:59

标签: node.js koa bookshelf.js

我是Node,Koa和Web开发的新手,我正在尝试将查询中的数据发送到模板。我正在使用Bookshelfjs作为ORM。

CellBeginEdit

1 个答案:

答案 0 :(得分:0)

您需要使用他们的Promise界面。我不确定findById是什么,所以我会从他们的文档中修改一个例子。

module.exports = {

    /** Get user info by id */
    getId: function * getId (next) {

        // assume User is a bookshelf model.
        // probably userMng in your case, but it's hard to say without more code.
        let user = yield User.where('id', this.params.id).fetch();

        this.body = yield this.render('id', {
            title: 'Show detailes about item: ' + this.params.id,
            data: user
        });

        yield next;
    }
};