2017-08-16 17 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中間件之一不是異步函數。以下將不會正常處理。我的一個功能不是異步功能導致錯誤。