我正在使用KnexJS promise查询数据库。我可以在async/await
中获得结果,但是当我使用Knex Promise时,无法访问Promise的ctx
和then
内的catch
对象。 console.log(results)
在控制台中显示结果。
这是我的代码:
router.get('/profile', async ctx => {
'http://localhost:8000/profile'
ctx.knex
.select().from('profile')
.then(results => {
console.error(results)
this.body = results
})
.catch(error => {
console.log(error)
this.throw(500, "Inside Error")
})
})
答案 0 :(得分:1)
ctx
应该可用,因为它是在封闭范围内设置的。
但是您必须从路由器处理程序中返回knex
的诺言:
router.get('/profile', ctx => {
return ctx.knex
.select().from('profile')
.then(results => {
console.error(results)
this.body = results
})
.catch(error => {
console.log(error)
this.throw(500, "Inside Error")
})
});