TypeError:callback.apply不是函数(Node.js& Mongodb) [英] TypeError: callback.apply is not a function (Node.js & Mongodb)

查看:100
本文介绍了TypeError:callback.apply不是函数(Node.js& Mongodb)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加{upsert:true}行时,我收到此错误:

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

TypeError:callback.apply不是函数

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

        });
    })

如果没有这一行,则没有错误。我是nodejs的新手,不太确定问题出在哪里。

Without this line, there is no error. I'm new to nodejs, not very sure where the problem is.

更新

现在没有错误信息,但这部分数据库是没有更新新数据。嵌入的文档仍为空。

No error message now, but this part of the database is not updated with new data. The embedded 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!' });
        });

})


推荐答案

那里有两种方法来更新mongodb中的文档:

There are 2 ways to update documents in mongodb:


  1. 找到文档,将其带到服务器,更改它,然后保存回来到mongodb。

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

只需给mongodb指令找到文件,改变它;然后在mongodb完成后最终返回结果/错误作为回调。

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. 使用user.save(),首先搜索数据库使用user.findOne,并将其拉到服务器(nodejs),现在它存在于您的计算机内存中。
    然后你可以手动更改数据,最后用user.save()保存到mongodb。

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


  • 第二个是使用User.findOneAndUpdate()..这是首选,而不是user.findOne()然后user.update();因为那些基本上搜索数据库两次。首先找到一个(),再次搜索更新()

  • 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()

    无论如何,秒ond方法告诉mongodb更新数据而不先带到服务器,接下来,只有在mongodb完成其操作后,你才会收到更新文件(或错误)作为回调

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

    这篇关于TypeError:callback.apply不是函数(Node.js& Mongodb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆