Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda