ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.58k stars 1.33k forks source link

Cannot read property 'request' of undefined #1592

Open ygtzz opened 4 years ago

ygtzz commented 4 years ago

while I request a file from a server in koa

router.post('/proxy', async function (ctx) {
    let postParam = ctx.request.body;
    let fileName = urlencode.decode(postParam.fileName);
    let filePath = urlencode.decode(postParam.filePath);
    filePaths = filePath.split('/');
    filePaths = filePaths.slice(0,-1);
    filePath = filePaths.join('/') + '/' + postParam.fileName;
    console.log('filePath: ', filePath)

    let res = superagent.get(postParam.filePath);
    let passThroughtStream = new stream.PassThrough();
    res.pipe(passThroughtStream);

    ctx.set("Content-disposition", "attachment; filename=" + postParam.fileName);
    ctx.type = filePath.split('.')[1];
    ctx.body = passThroughtStream;
});

superagent makes error:

TypeError: Cannot read property 'request' of undefined
      at Request.request (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_superagent@6.1.0@superagent/lib/node/index.js:795:18)
      at Request.end (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_superagent@6.1.0@superagent/lib/node/index.js:951:8)
      at Request.pipe (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_superagent@6.1.0@superagent/lib/node/index.js:434:8)
      at /Users/mengff/Documents/workspace/github/download-demo/index.js:66:9
      at dispatch (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-compose@4.1.0@koa-compose/index.js:42:32)
      at /Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-router@9.4.0@koa-router/lib/router.js:372:16
      at dispatch (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-compose@4.1.0@koa-compose/index.js:42:32)
      at /Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-compose@4.1.0@koa-compose/index.js:34:12
      at dispatch (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-router@9.4.0@koa-router/lib/router.js:377:31)
      at dispatch (/Users/mengff/Documents/workspace/github/download-demo/node_modules/_koa-compose@4.1.0@koa-compose/index.js:42:32)