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

1.2K
Vistas
findOneAndUpdate, Callback must be a function, got [object Object] , NodeJs, mongoose error

I am developing a webapp in MERN stack, while doing so I encountered with an error , please help. When I try to update the user it gives me an error, Callback must be a function, got '[object Object]'.

This is the API.

It is guaranteed that user always exists.

module.exports.addBlog=(req,res)=>{
    const {title,data} = req.body.blog;
    console.log(title,data);
    const {email} = req.body;
    const newblog ={
        title,
        data
    }
    userSchema.findOneAndUpdate(
        {email},
        {$push:{blogArray:newblog}},
        {upsert:true},
        {useFindAndModify:false}
        )
        .then(res=>console.log(res))
        .catch(err=>console.log(err));
   
    return res.json({msg:"Blog added successfully"});
}

This is the userschema.

const userSchema = new mongoose.Schema({
    firstname:{
        type:String,
        required:true,
        lowercase:true
    },
    lastname:{
        type:String,
        required:true,
        lowercase:true
    },
    email:{
        type:String,
        required:true,
        lowercase:true
    },
    password:{
        type:String,
        required:true
    },
   blogArray:[
       {
        title:{
            type:String,
             required:true
        },
        data:{
            type:String,
            required:true
        }
    }
   ]
});
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

findOneAndUpdate has 3 possible signatures:

    findOneAndUpdate(
        filter: FilterQuery<TSchema>,
        update: UpdateQuery<TSchema> | TSchema,
        callback: MongoCallback<FindAndModifyWriteOpResultObject<TSchema>>,
    ): void;
    findOneAndUpdate(
        filter: FilterQuery<TSchema>,
        update: UpdateQuery<TSchema> | TSchema,
        options?: FindOneAndUpdateOption<TSchema>,
    ): Promise<FindAndModifyWriteOpResultObject<TSchema>>;
 findOneAndUpdate(
        filter: FilterQuery<TSchema>,
        update: UpdateQuery<TSchema> | TSchema,
        options: FindOneAndUpdateOption<TSchema>,
        callback: MongoCallback<FindAndModifyWriteOpResultObject<TSchema>>,
    ): void;

You're trying to use signature #2 the one that utilizes a promise, So you need to change your syntax to:

userSchema.findOneAndUpdate(
        {email},
        {$push:{blogArray:newblog}},
        {upsert:true, useFindAndModify:false},
        )
        .then(res=>console.log(res))
        .catch(err=>console.log(err));

view all Mongos nodejs types here

over 4 years ago · Santiago Trujillo 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