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

155
Vistas
static is not a function - mongoose

I get the error "product.try() is not a function"

If I immediately invoke the try() it works ok, and node recognize the model "product".

is there something I d'ont understand about how mongoose works? I run the whole code in the one file and I followed the steps of the tutor from the video in the course I take.

what is the problem? thank you.

file: product.js =>

 const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost:27017/shopApp')
    .then(()=>{
        console.log('CONNECTION OPEN!!!')
    })
    .catch(err=>{
        console.log('HO NO ERROR!!!')
        console.log(err)
    })
    
 
    const productSchema = new mongoose.Schema({
        name: {
            type: String,
            required: true,
            maxlength: 20   // Schema options
        },
        price: {
            type: Number, 
            min: [0, 'Price must be positive']
        },
        onSale: {
            type: Boolean,
            default: false
        },
        categories: {
        type: [String]
        }, 
        qty: {
            online: {
                type: Number,
                default: 0
            },
            inStore: {
                type: Number,
                default: 0
            }
        },
        size: {
            type: String,
            enum: ['S', 'M', 'L'] 
        }
    })
    
    const product = mongoose.model('product', productSchema); 
    
    productSchema.statics.try = function(){
        console.log('OK')
    };
    
    product.try();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You actually need tadd the .try function before calling mongoose.model()

This should work for you:

// connect to mongose...
// ... 

const productSchema = new mongoose.Schema({ /* .. your schema ..  */})
 
// add the function to the schema before creating the model!
productSchema.statics.try = function () {
  console.log('OK')
};


// since everything is added create the product model!
const product = mongoose.model('product', productSchema);

product.try();  // should work now.

Note: The .model() function makes a copy of schema. Make sure that you've added everything you want to schema, including hooks, before calling .model()!

docs

  • important note
  • static method reference
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