Koa中的asynchronous函数奇怪的问题

这是代码片段:

//Category.service.js ... exports.update = async (ctx, next) => { const {categoryId} = ctx.params const _category = ctx.request.body // ctx.body = {key: 'test'} const category = await Category.get(categoryId) console.log(category) ctx.body = await category.update(_category) console.log(ctx.response) } ... 

当我发送请求时,它返回“未find”。 但terminal打印正确的结果:

 Category { id: 1, name: 'Javascript', description: 'This is a test for category update.' } { status: 404, message: 'Not Found', header: { 'x-dns-prefetch-control': 'off', 'x-frame-options': 'SAMEORIGIN', 'strict-transport-security': 'max-age=15552000; includeSubDomains', 'x-download-options': 'noopen', 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', vary: 'Accept-Encoding, Origin', 'access-control-allow-origin': 'chrome- extension://fhbjgbiflinjbdggehcddcbncdddomop', 'content-type': 'application/json; charset=utf-8', 'content-length': '21' }, body: Category { id: 1, name: 'Javascript', description: 'This is a test for category update.' } } 

唯一的问题是状态是404,但是当我尝试这个时:

 //Category.service.js ... exports.update = async (ctx, next) => { const {categoryId} = ctx.params const _category = ctx.request.body ctx.body = {key: 'test'} // const category = await Category.get(categoryId) // console.log(category) // ctx.body = await category.update(_category) // console.log(ctx.response) } ... 

一切工作正常。 这是该项目的链接。 我不知道代码有什么问题。

如果koa中间件之一不是asynchronousfunction。 以下将不会正常处理。 我的一个函数不是asynchronous函数导致的错误。