我如何从 Koa 中的数据库查询访问结果?
How I access results from a db query in Koa?
我是 Node、Koa 和 Web 开发的新手,我正在尝试将数据从查询发送到模板。我正在使用 Bookshelfjs 作为 ORM。
module.exports = {
/** Get user info by id */
getId: function * getId (next) {
// var user = { username: 'lucas', passowrd: 'lucas' };
userMng.findById(this.params.id, (err, user) => {
this.body = yield this.render('id', {
title: 'Show detailes about item: ' + this.params.id,
data: user
});
});
yield next;
}
}
您需要使用他们的 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;
}
};
我是 Node、Koa 和 Web 开发的新手,我正在尝试将数据从查询发送到模板。我正在使用 Bookshelfjs 作为 ORM。
module.exports = {
/** Get user info by id */
getId: function * getId (next) {
// var user = { username: 'lucas', passowrd: 'lucas' };
userMng.findById(this.params.id, (err, user) => {
this.body = yield this.render('id', {
title: 'Show detailes about item: ' + this.params.id,
data: user
});
});
yield next;
}
}
您需要使用他们的 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;
}
};