2017-08-16 15 views
1

异步功能奇怪的问题下面是代码片段:与兴亚

//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) 
} 
... 

当我发送一个请求,返回“未找到”。然而,终端打印出正确的结果:

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) 
} 
... 

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

回答

1

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