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

314
Vistas
mongoose: save is not a function

Given a user model:

import { model, Schema } from 'mongoose'

export interface User {
  email: string
}

const userSchema = new Schema<User>(
  {
    email: {
      type: String,
      required: true,
    },
  },
)

export const UserModel = model<User>('User', userSchema)

I'm trying to save it as so:

// inside an async function
const newUser: HydratedDocument<User> = new UserModel({
  email: 'aaa@aaa.com',
})

console.log(newUser)

await newUser.save()

Which results in newUser.save is not a function. What am I missing? Also, here is the output of the `console.log(newUser)

Also, here is the output of the `console.log(newUser)

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

0

My stupid mistake: I was calling everything on the fronted. Solution is as easy as moving relevant db calls to the endpoint.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You will need to also define a model, change your schema definition to this:

first create model from Schema :

 var UserModel = mongoose.model('User', User);

then create object out of User model

 var user = new UserModel(req.body)

then call

 user.save(function(){});

check documentation http://mongoosejs.com/docs/api.html#model_Model-save

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