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

When I add the line "{upsert: true}", I got this error:

TypeError: callback.apply is not a function

// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')

    // update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
    .post(function(req, res) {

        // use our user model to find the user we want
        User.findOne({ userName: req.params.userName}, function(err, user) {

            if (err)
                res.send(err);

            console.log('user.competitorAnalysis.firstObservation: %@', user.competitorAnalysis.firstObservation);
            // Got the user name
            var userName = user.userName;
            // update the text data
            console.log('Baobao is here!');
            user.update(
                {
                    userName: 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 }
            );

            // save the user
            user.save(function(err) {
                if (err)
                    return res.send(err);

                return res.json({ message: 'User updated!' });
            });

        });
    })

There are no errors without this line. I am new to nodejs, not very sure where the problem is.

Update

There is no error message, but this part of the database is not updated with new data. The attached document is still empty.

// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')

// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
.post(function(req, res) {

    console.log('1');

    // Just give instruction to mongodb to find document, change it;
    // then finally after mongodb is done, return the result/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
            console.log('2');
            // now you can send the error or updated file to client
            if (err)
                return res.send(err);

            return res.json({ message: 'User updated!' });
        });

})
+4
source share
2 answers

There are two ways to update documents in mongodb:

  • find the document, return it to the server, change it and save it back to mongodb.

  • mongodb , ; , , mongodb, / .

.


  • user.save(), user.findOne (nodejs), . , , mongodb 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!' });
        });
    
  • - User.findOneAndUpdate(). , user.findOne(), user.update(); . One() ()

, mongodb . , mongodb ( )

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!' });
        });
+8

update

  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 3 .

+2

Source: https://habr.com/ru/post/1653482/


All Articles