TypeError: callback.apply is not a function (Node.js & Mongodb) TypeError: callback.apply is not a function (Node.js & Mongodb) mongodb mongodb

TypeError: callback.apply is not a function (Node.js & Mongodb)


There are 2 ways to update documents in mongodb:

  1. find the document, bring it to server, change it, then save it back to mongodb.

  2. just give instruction to mongodb to find document, change it; then finally after mongodb is done, return the result/error as callback.

In your code, you are mixing both methods.


  1. with user.save(), first you search the database with user.findOne, and pull it to server(nodejs), now it lives in your computer memory. then you can manually change the data and finally save it to mongodb with user.save()

    User.findOne({ userName: req.params.userName}, function(err, user) {    if (err)        res.send(err);    //this user now lives in your memory, you can manually edit it    user.username = "somename";    user.competitorAnalysis.firstObservation = "somethingelse";    // after you finish editing, you can save it to database or send it to client     user.save(function(err) {        if (err)            return res.send(err);        return res.json({ message: 'User updated!' });    });
  2. the second one is to use User.findOneAndUpdate().. This is preferred, instead of user.findOne() then user.update(); because those basically searching the database twice. first to findOne(), and search again to update()

Anyway,the second method is telling mongodb to update the data without first bringing to server, Next, only after mongodb finish with its action, you will receive the updated-file (or error) as callback

User.findOneAndUpdate({ userName: req.params.userName},             {             $set: { "competitorAnalysis.firstObservation" : req.body.firstObservation,                      "competitorAnalysis.secondObservation" : req.body.secondObservation,                      "competitorAnalysis.thirdObservation" : req.body.thirdObservation,                      "competitorAnalysis.brandName" : req.body.brandName,                      "competitorAnalysis.productCategory" : req.body.productCategory            } },            { upsert: true },        function(err, user) {        //after mongodb is done updating, you are receiving the updated file as callback            // now you can send the error or updated file to client        if (err)            res.send(err);        return res.json({ message: 'User updated!' });        });


You forgot to pass a callback to the update method

user.update(  {    $set: {      'competitorAnalysis.firstObservation': req.body.firstObservation,      'competitorAnalysis.secondObservation': req.body.secondObservation,      'competitorAnalysis.thirdObservation': req.body.thirdObservation,      'competitorAnalysis.brandName': req.body.brandName,      'competitorAnalysis.productCategory': req.body.productCategory,    },  },  {upsert: true},  function (err, result) {},);

update method expects 3 arguments.

  • document update
  • options
  • callback