fastify-overview icon indicating copy to clipboard operation
fastify-overview copied to clipboard

TypeError: Cannot read properties of undefined (reading 'func')

Open nasraldin opened this issue 5 months ago • 16 comments

Hello team,

Please apply the fix to avoid the below issue that throw when using with typescript.

Current ver.

"fastify-overview": "3.7.0",
 "fastify": "4.26.0",

To reproduce the issue: add the plugin pnpm add fastify-overview

register to fastify 

import FastifyOverview from 'fastify-overview';
      fastify.register(FastifyOverview, {
        addSource: true, // current this opt is throw error and stoped the app start
        exposeRoute: true,
        exposeRouteOptions: {
          url: '/json-overview',
        },
      });

run your app

result application stoped to start and throw below error

Failed to start application: TypeError: Cannot read properties of undefined (reading 'func')
    at Boot.manInTheMiddle (/api/node_modules/.pnpm/[email protected]/node_modules/fastify-overview/index.js:90:107)
    at Boot.markInstance (/api/node_modules/.pnpm/[email protected]/node_modules/fastify-overview/index.js:30:20)
    at Boot.override (/api/node_modules/.pnpm/[email protected]/node_modules/fastify/lib/pluginOverride.js:69:56)
    at Plugin.exec (/api/node_modules/.pnpm/[email protected]/node_modules/avvio/plugin.js:79:33)
    at Boot.loadPlugin (/api/node_modules/.pnpm/[email protected]/node_modules/avvio/plugin.js:272:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

The fix:

I'm just checking in the compiled source code in node modules of your lib to fix but for you you need to fix the root cause in js that fun related to show source

In the manInTheMiddle function, update the check to ensure that _current has a value before assigning it to the source.

old fun is

if (opts.addSource && this) {
    trackStructure.source = this._current.find(loadPipe => loadPipe.func[kSourceRegister] !== undefined).func[kSourceRegister]
}

new code

if (opts.addSource && this) {
  const currentSourcePipe = this._current.find(loadPipe => loadPipe.func[kSourceRegister] !== undefined);
  if (currentSourcePipe) {
    trackStructure.source = currentSourcePipe.func[kSourceRegister];
  }
}

Thank you for creating such an amazing tool!

nasraldin avatar Feb 02 '24 21:02 nasraldin