Search code examples
javascriptnode.jsjsontypeerrorfastify

Fastify - TypeError: Cannot read property 'length' of undefined


When i created a new API with fastify, while testing it, it was throwing

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)

The control doesn't even pass to the routes and when i checked the file mentioned in the stack trace, it seems to be related with preParser


Solution

  • It was apparently a JSON Schema issue.

    In one of the routes, I had the schema like this,

    {
      type: "object",
      required: ["fileType"],
      properties: {
        fileType: {
          type: {
             type: "string",
             enum: ["a", "b", "c", "d", "all"]
          }
        }
      }
    }
    

    Which is wrong, and it should be,

    {
      type: "object",
      required: ["fileType"],
      properties: {
        fileType: {
          type: "string",
          enum: ["a", "b", "c", "d", "all"]
        }
      }
    }