Makarand
Makarand

Reputation: 606

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
        }
    }
   ]
});

Upvotes: 2

Views: 1697

Answers (2)

fruitloaf
fruitloaf

Reputation: 2892

this is because you need to put ALL of your 'updating operations' into second object.

replication of error:

findOneAndUpdate({id: mongoId}, {$inc: {total: 10}}, {$inc: {withdrawals: -10}}, {new: true})

result: ERROR

FIX:

move ALL of your $INC operations into 2ND ARGUMENT OBJECT:

findOneAndUpdate({id: mongoId}, {$inc: {total: 10} $inc: {withdrawals: -10}}, {new: true})

RESULT: works

basically ALL of your operations must go as a second argument and must ALL be in a single object, like this:

{
 $inc: {total: 10},
 $inc: {withdrawals: -10}
}

DO NOT make separate object per operation, otherwise you will get this error, i.e.

{
 $inc: {total: 10}
}, 
{
 $inc: {withdrawals: -10}
}

Here is video on my channel where I show you how to fix it: https://youtu.be/nPC1OAGXGWM

Upvotes: 1

Tom Slabbaert
Tom Slabbaert

Reputation: 22296

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

Upvotes: 4

Related Questions