2

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
        }
    }
   ]
});
1
  • 1
    You're passing four separate objects to findOneAndUpdate, I don't think that's what you intended. Commented Feb 25, 2021 at 14:06

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.