Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
Mongoose global plugins not working (ES6)

I'm running into an issue with mongoose.plugin() and none of global plugin changes seem to be affecting the schemas.

Other than importing, I've tried directly writing the plugin in the same index.js file, and removing pointer functions. The global plugins just don't seem to be taking effect. I'm just trying to apply toJSON: { virtuals: true } to all models.

"mongoose": "^6.0.12"
import test from './plugins/test.js'
...

// Plugins
mongoose.plugin(test)
...

// Models
import './user.js'
...

test.js content:

export default (schema) => {
  schema.set('toJSON', { ...schema.get('toJSON'), virtuals: true })

  return schema
}

When I directly apply { toJSON: { virtuals: true } } to the user schema it works as intended, but I want to apply this and potentially other plugins to all the models.

Any help or information is appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I got it working by up splitting plugins and models into separate files and created an index.js for importing all the plugins.

Within the app.js now I import plugins first followed by the models:

...
import mongoose from 'mongoose'
import './models/plugins'
import './models
...
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda