Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Property 'db' does not exist on type 'FastifyInstance<Server

//util file

async function dbconnector(fastify, options) {
  try {
    await client.connect()
    console.log('db connected succesfully')
    fastify.decorate('db', { client })
  } catch(err) {
    console.error(err)
  }
}
module.exports = fastifyPlugin(dbconnector)

//index file

const dbconnector = require('./utils/db')
fastify.register(dbconnector)

whenever I try to do fastify.db it shows the below error

Property 'db' does not exist on type 'FastifyInstance<Server, IncomingMessage, ServerResponse, FastifyLoggerInstance> & PromiseLike

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With the way Fastify have set up it's types you need to add the decorators yourself to the FastifyInstance. You can do that like so:

import { FastifyLoggerInstance, FastifyPluginAsync, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from 'fastify'

declare module 'fastify' {
  export interface FastifyInstance<
  RawServer extends RawServerBase = RawServerDefault,
  RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
  Logger = FastifyLoggerInstance
> {
    db: FastifyPluginAsync;
  }
}

You need to tell typescript to pick up this type file if you haven't already set it up in your tsconfig.json. An example of doing that would be this:

{
  "compilerOptions": {
    // ... your other config
    "typeRoots": [
      "types"
    ]
  },
  // ... the rest of your config
}

Then save the configuration in the root of your project under types/index.d.ts.

This tells the typescript compiler that you have defined your own types in that file. Extending the FastifyInstance to include your decorator should now work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!