fastify / help

Need help with Fastify? File an Issue here.
https://www.fastify.io/
65 stars 8 forks source link

TypeError: Cannot read property 'length' of undefined #289

Closed Skn0tt closed 3 years ago

Skn0tt commented 4 years ago

🐛 Bug Report

I'm trying to get the Schema validation working, but Fastify throws whenever I make a request:

TypeError: Cannot read property 'length' of undefined

      at next (node_modules/fastify/lib/route.js:407:32)
      at preParsingHookRunner (node_modules/fastify/lib/route.js:438:3)
      at runPreParsing (node_modules/fastify/lib/route.js:389:5)
      at Object.routeHandler [as handler] (node_modules/fastify/lib/route.js:349:7)
      at Router.lookup (node_modules/find-my-way/index.js:356:14)

To Reproduce

  1. Clone https://github.com/quirrel-dev/quirrel/tree/transition-to-owl
  2. Go to /api and run npm test (the tests require Redis running on localhost:6379, you can start one using docker run -d --name redis -p 6379:6379 redis)

Expected behavior

I expect Fastify to successfully answer my requests.

Your Environment

Skn0tt commented 4 years ago

@Ethan-Arrowood asked if it works without the Schema: Yes, it does.

It seems to me that in this line, context.preParsing is undefined in my case.

As a workaround, I was able to fix this issue by adding this.preParsing = null in context.js.

Ethan-Arrowood commented 4 years ago

So I think this is an issue with the import. Why are you using import * as thatSchema from './that.json'? I'd debug and make sure this is importing as expected.

Skn0tt commented 4 years ago

I've added console.log({ EndpointJobIDParamsSchema }) to queues.ts and it logged the following:

{
  EndpointJobIDParamsSchema: {
      '$schema': 'http://json-schema.org/draft-07/schema#',
      title: '/queues/:endpoint/:id params',
      type: 'object',
      properties: { id: [Object], endpoint: [Object] },
      required: [ 'id', 'endpoint' ],
      additionalProperties: false
    }
}

That seems correct to me. What do you think?

mcollina commented 3 years ago

@Skn0tt I have tried and I cannot reproduce based on the provided instructions. Could you include a simplified example in a single file? Thanks

Ethan-Arrowood commented 3 years ago

Yes this is what I suspected @Skn0tt - you might need to pass thatSchema.EndpointJobIDParamsSchema to the route. But also a more simplified example can help us narrow down the issue you're facing

Skn0tt commented 3 years ago

That hasn't been the problem, apparently. It was that I specified my response schema like so in another route:

fastify.get("/anything", { schema: { response: thatSchema } }, ...)

instead of

fastify.get("/anything", { schema: { response: { 200: thatSchema } } }, ...)

It makes sense in hindsight, but I wasn't able to find the error by looking at the error messages.

I'll provide a minimal repro nonetheless and check if I can provide a PR that makes the developer mistake easier to spot :D