首页 文章

Koa每次发送状态404

提问于
浏览
2
export async function getPlaces(ctx, next) {
    const { error, data } = await PlaceModel.getPlaces(ctx.query);
    console.log(error, data);
    if (error) {
        return ctx.throw(422, error);
    }
    ctx.body = data;
}

Koa每次发送404状态和空身,我做错了什么?

3 回答

  • 0

    你必须用路由器连接你的功能 . 以下是它的工作原理:

    import * as Koa from "koa";
    import * as Router from "koa-router";
    
    let app = new Koa();
    let router = new Router();
    
    async function ping(ctx) {
      ctx.body = "pong";
      ctx.status = 200;
    }
    
    router.get("/ping", ping);
    
    app.use(router.routes());
    app.listen(8080);
    
  • 2

    我也有这个问题,并通过添加:

    ctx.status = 200;

    直接在下面

    ctx.body = data;

  • 0

    在看来, await 并不真的"wait"因此返回得太早(这会导致404错误) .

    其中一个原因可能是你的 PlaceModel.getPlaces(ctx.query) 没有返回一个承诺 . 所以它继续而不等待 getPlaces 的结果 .

相关问题