DoraJDJ
V2EX  ›  Node.js

koa-router 是否真的能正常使用 async/await ?

  •  1
     
  •   DoraJDJ · Apr 15, 2017 · 6025 views
    This topic created in 3316 days ago, the information mentioned may be changed or developed.

    刚才试着用 Koa 2 写个应用,结果在使用 async/await 的时候有点迷糊。

    我给自己的应用用 koa-router 这个中间件写了个路由,然后从远程 API 服务器上获取信息:

    router.get('/bili/:vid', async (ctx, next) => {
        try {
            var result = await services.bilibili.video(this.params.vid);
            ctx.body = result;
        } catch (e) {
            ctx.body = e;
        }
    });
    

    但是在运行测试的时候发现它返回的内容直接是一个空对象,什么都没有,错误也没报:

    HTTP GET: /bili/1145140
    Response: {}
    

    后来我不使用 router 中间件直接 app.use 发现功能一切正常:

    app.use(async (ctx) => {
        ctx.body = await services.bilibili.video(1145140);
    });
    
    Response: {"tid":59,"typename":"演奏",...(如下略过)
    

    我确定在远程服务器上获取信息的函数返回的是 Promise 对象:

    function getVideoInfo(vid) {
        return new Promise((resolve, reject) => {
            var reqmap = new Map();
            reqmap.set('appkey', config.biliapi.appkey);
            reqmap.set('id', vid);
            reqmap.set('page', 1);
            ...(如下略过)
    

    发现问题后我尝试在 Google 和 GitHub 上查找相关关键词但都没有找到类似的案例,不知道是什么问题造成的。

    Koa 版本是 2.2.0 , Koa router 版本是 7.1.1 。

    Supplement 1  ·  Apr 16, 2017
    老了,不中用了

    是 ctx.params 不是 this.params ,写 Koa 1 代码写中毒了...
    11 replies    2017-04-16 12:05:24 +08:00
    int64ago
        1
    int64ago  
       Apr 15, 2017 via Android
    所以 router 外部逻辑呢(你只贴了内部逻辑)?一起贴出来
    DoraJDJ
        2
    DoraJDJ  
    OP
       Apr 15, 2017
    @int64ago
    小的不太了解专业术语,不知道外部逻辑和内部逻辑是什么意思,请见谅。

    const Koa = require('koa');
    const render = require('koa-ejs');
    const koaStatic = require('koa-static');
    const path = require('path');

    const app = new Koa();
    render(app, {
    root: path.join(__dirname, 'views'),
    layout: false,
    viewExt: 'ejs'
    });

    app.use(koaStatic(path.join(__dirname, 'statics'))).use(routers.routes()).use(routers.allowedMethods());

    使用到的中间件也就这些了。
    zbinlin
        3
    zbinlin  
       Apr 15, 2017 via Android
    用了 arrow function 后 this 不是 router 实例了
    DoraJDJ
        4
    DoraJDJ  
    OP
       Apr 15, 2017
    @zbinlin 刚才把箭头函数打掉了,但返回的内容照样是空对象。
    zbinlin
        5
    zbinlin  
       Apr 16, 2017 via Android
    @DoraJDJ 先 use 里那样,直接写死一个 id 试下
    zbinlin
        6
    zbinlin  
       Apr 16, 2017 via Android
    @zbinlin s/先 /像
    Mikewu
        7
    Mikewu  
       Apr 16, 2017
    你使用 koa-router , await services.bilibili.video(this.params.vid)的参数是 String ,返回结果的是空{},并没有捕获到异常。
    你直接 app.user, await services.bilibili.video(1145140)的参数是 Number ,正确返回结果。
    那是不是你的 services 方法有问题呢? 方便的话贴出来看看。
    或者直接自己打断点 debug 吧, 顺便说下 koa-router@next 是肯定能正常使用 async/await
    DoraJDJ
        8
    DoraJDJ  
    OP
       Apr 16, 2017
    @zbinlin 果然我又不中用了...
    刚才写死 id 之后内容获取正常。
    再重新看了下 koa-router 的示例代码,我居然模仿的是旧版本的示例...
    binux
        9
    binux  
       Apr 16, 2017
    打 log 啊
    hansnow
        10
    hansnow  
       Apr 16, 2017
    遇到过很多次类型情况,让我养成一个习惯:先怀疑自己,再怀疑框架
    songjiaxin2008
        11
    songjiaxin2008  
       Apr 16, 2017 via iPhone
    当然可以 https://github.com/solarhell/journey/blob/master/src/routes.ts

    可以在看下 CheckPermission 的写法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2938 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 11:32 · PVG 19:32 · LAX 04:32 · JFK 07:32
    ♥ Do have faith in what you're doing.