这是路线的代码。当我使用 commentedPromise
时,它在正文中返回 123。但是使用猫鼬查询它会返回 404 状态。日志中的项目发现良好。但似乎路由器只是忽略等待并立即返回 404。我究竟做错了什么?
router.get('/:id', async (ctx, next) => {
// var item = await Promise.resolve(123); // this line works good!
var item = await Model.findById(ctx.params.id); // but this not
ctx.body = item;
console.log('hmm', item, ctx.response);
});
在 console.log 输出一切都很好,但404 Not Found
像默认的 koa 响应一样抛出:
hmm { _id: 5accda0700c0afd3ca50bc67,
name: 'yuki 2',
server: 5accd5848ae2e2d2be1760c6,
owner: 5accd023cc3a90d1f73d4afd,
createdAt: 2018-04-10T15:36:39.965Z,
updatedAt: 2018-04-10T15:36:39.965Z,
__v: 0 }
{ status: 404,
message: 'Not Found',
header:
{ 'access-control-allow-credentials': 'true',
'content-type': 'text/plain; charset=utf-8',
'content-length': '9' },
body: { _id: 5accda0700c0afd3ca50bc67,
name: 'yuki 2',
server: 5accd5848ae2e2d2be1760c6,
owner: 5accd023cc3a90d1f73d4afd,
createdAt: 2018-04-10T15:36:39.965Z,
updatedAt: 2018-04-10T15:36:39.965Z,
__v: 0 } }