【问题标题】:callback.apply is not afunction in node jscallback.apply 不是节点 js 中的函数
【发布时间】:2017-07-28 17:35:44
【问题描述】:

为了解决我在How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist? 中的问题,我编写了以下代码。

但这给出了一个错误提示 TypeError: callback.apply is not a function。我对此不太熟悉。谁能解决这个错误?

代码

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("/");
    }
 );

});

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您有一个导致问题的额外查询参数。将其移动到第一个查询对象为:

    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("/");
            }
        );
    });
    

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2019-12-05
      • 2016-10-27
      • 2016-03-14
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多