raw-body icon indicating copy to clipboard operation
raw-body copied to clipboard

TypeError: stream.on is not a function

Open dawadam opened this issue 2 years ago • 2 comments

Hello, I have this error, I am using Koa middleware.

TypeError: stream.on is not a function
    at readStream (/node_modules/raw-body/index.js:197:10)
    at executor (/node_modules/raw-body/index.js:113:5)
    at new Promise (<anonymous>)
    at getRawBody (/node_modules/raw-body/index.js:112:10)
    at /src/index.ts:99:30
    at step (/src/index.ts:33:23)
    at Object.next (/src/index.ts:14:53)
    at /src/index.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/src/index.ts:4:12)
    at /src/index.ts:95:29
    at dispatch (/node_modules/koa-compose/index.js:42:32)
    at filter (/node_modules/koa-json/index.js:25:12)
    at dispatch (/node_modules/koa-compose/index.js:42:32)
    at cors (/node_modules/@koa/cors/index.js:59:38)
    at dispatch (/node_modules/koa-compose/index.js:42:32)

My code is :

var contentType = require('content-type')
var getRawBody = require('raw-body')

    app.use(async (ctx, next) => {

        if (ctx.request.originalUrl.startsWith('/raw-parse')) {

            ctx.text = await getRawBody(ctx.request, {
                length: ctx.request.headers['content-length'],
                limit: '1mb',
                encoding: contentType.parse(ctx.request).parameters.charset
            })
        }

        await next();
    })

dawadam avatar May 25 '22 22:05 dawadam

Hello, and sorry for that problem. It looks like the argument you are passing into this module, ctx.request is not a stream. We can improve the error message this module gives instead of assuming a stream was passed 👍

dougwilson avatar May 25 '22 22:05 dougwilson

Ok I see. I don't know how to retrieve the Koa request stream. But you put an example with Koa in the Readme, does it work? Because I don't understand it.

Edit: I think i'm juste need to change ctx.request by ctx.req Thank you for the quick reply

dawadam avatar May 26 '22 07:05 dawadam