node.js - callback.apply 不是 Node js 中的函数

标签 node.js mongodb mongoose

作为我在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。我对此不太熟悉。有人能解决这个错误吗?

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

});

最佳答案

您有一个额外的查询参数导致了该问题。将其移动到第一个查询对象,如下所示:

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

关于node.js - callback.apply 不是 Node js 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666458/

相关文章:

node.js - Mongodb返回字符串数组而不是对象数组

node.js - 请求- promise 下载 pdf 文件

node.js - Mongoose 具有异步队列和 waterfall

node.js - 如何更改 Node 中 PCM 音频流的音量?

mongodb - 如何通过mongodb中的两个键删除重复条目?

mongodb - Apache Camel 读取 MongoDB 集合 - 不处理任何行

node.js - 如何制作和存储帖子标题的子弹?

javascript - 这个 JavaScript 对象是如何转换为字符串的?

node.js - 无法将 mongoose 插件连接到 Express 中的 mongodb

JavaScript NodeJs 开放库