1

As a solution to my question in How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist? I wrote the following code.

But this gives an error saying TypeError: callback.apply is not a function. I am not so familiar with this. Can anyone solve this error?

code

router.post("/saveMCQAnswer", function(req, res) { 

Survey.findOneAndUpdate(
     { "_id": '0001'},
    { "surveyname": 's1' }, /* <query> */
    { /* <update> */
        "$push": {
            "replies": {
                "_id" : 'R001',
                  "answers": {
                    "_id" : 'A001',
                    "answer" : 'answer'
                  }
            }
        } 
    },
    { "upsert": true }, /* <options> */
    function(err, doc){ /* <callback> */
        if(err) res.json(err);
        else
            req.flash('success_msg', 'Question saved to QBank');  
        res.redirect("/");
    }
 );

});
Community
  • 1
  • 1
Kabilesh
  • 1,000
  • 6
  • 22
  • 47

1 Answers1

3

You have an extra query parameter that is causing the issue. Move it to the first query object as:

router.post("/saveMCQAnswer", function(req, res) { 
    Survey.findOneAndUpdate(
        { /* <query> */
            "_id": '0001',
            "surveyname": 's1' // <-- fix here
        }, 
        { /* <update> */
            "$push": {
                "replies": {
                    "_id" : 'R001',
                    "answers": {
                        "_id" : 'A001',
                        "answer" : 'answer'
                    }
                }
            } 
        },
        { "upsert": true }, /* <options> */
        function(err, doc){ /* <callback> */
            if(err) res.json(err);
            else
                req.flash('success_msg', 'Question saved to QBank');  
            res.redirect("/");
        }
    );
});
chridam
  • 100,957
  • 23
  • 236
  • 235