javascript - 类型错误 : Cannot read property 'apply' of undefined at EventEmitter

标签 javascript node.js express mongoskin dhtmlx-scheduler

我正在构建一个 Node js 应用程序,其中一个页面是 DHTMLX 调度程序。但是,无论何时加载页面,应用程序都会崩溃,并且出现以下错误:

TypeError:无法读取 EventEmitter 处未定义的属性“应用”。(/node_modules/mongoskin/lib/collection.js:52:21))

这是错误引用的模块:

  SkinCollection.prototype._open = function(callback) {
    var collection_args = this._collection_args.concat([callback]);
    this._skin_db.open(function(err, db) {
        if(err) return callback(err);
        db.collection.apply(db, collection_args);
    });
  }

这是我的 app.js 中导致错误的部分,当我不包含此代码时,日历不会使应用程序崩溃,但我无法将数据发送到我的 MongoDB atlas 集群。

应用程序.js

    app.use(express.static(path.join(__dirname, 'public')));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));

    app.get('/init', function(req, res){
        db.event.insert({ 
            text:"My test event A", 
            start_date: new Date(2018,8,1),
            end_date:   new Date(2018,8,5)
        });
        db.event.insert({ 
            text:"My test event B", 
            start_date: new Date(2018,8,19),
            end_date:   new Date(2018,8,24)
        });
        db.event.insert({ 
            text:"Morning event", 
            start_date: new Date(2018,8,4,4,0),
            end_date:   new Date(2018,8,4,14,0)
        });
        db.event.insert({ 
            text:"One more test event", 
            start_date: new Date(2018,8,3),
            end_date:   new Date(2018,8,8),
            color: "#DD8616"
        });

        res.send("Test events were added to the database")
    });


    app.get('/data', function(req, res){
        db.event.find().toArray(function(err, data){
            //set id property for all records
            console.log(err);
            for (var i = 0; i < data.length; i++)
                data[i].id = data[i]._id;

            //output response
            res.send(data);
        });
    });


    app.post('/data', function(req, res){
        var data = req.body;
        var mode = data["!nativeeditor_status"];
        var sid = data.id;
        var tid = sid;

        delete data.id;
        delete data.gr_id;
        delete data["!nativeeditor_status"];


        function update_response(err, result){
            if (err)
                mode = "error";
            else if (mode == "inserted")
                tid = data._id;

            res.setHeader("Content-Type","application/json");
            res.send({action: mode, sid: sid, tid: tid});
        }

        if (mode == "updated")
            db.event.updateById( sid, data, update_response);
        else if (mode == "inserted")
            db.event.insert(data, update_response);
        else if (mode == "deleted")
            db.event.removeById( sid, update_response);
        else
            res.send("Not supported operation");
    });

过去一周我才开始学习 javascript,对编码还很陌生。我不明白为什么会出现此错误。当我将 Scheduler 应用程序作为独立功能运行时,它可以完美运行。只有当我尝试在我的网站中实现它时,它才会抛出此错误。

最佳答案

更新:正确答案

此问题与以下问题重复:MongoSkin "Cannot read property 'apply' of undefined"

Mongoskin只支持MongoDB 2.x,使用MondoDB 3.x时会出现这个错误。

原始(不正确)答案

我自己是 MongoDB 的新手,但我很确定问题在于您需要将 collection 替换为该行中表的实际名称:

db.collection.apply(db, collection_args);

例如,如果表/集合的名称是 skin,则该行将变为:

db.skin.apply(db, collection_args);

(注意当 app.j 对事件表执行数据库操作时,它是如何调用 db.event.insert() 等...)

关于javascript - 类型错误 : Cannot read property 'apply' of undefined at EventEmitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506180/

相关文章:

angularjs - 如何以 RESTful 方式在一个 API 请求中发布多个项目?

node.js - 使用服务器端渲染的最佳方式

javascript - foreach 循环中的未定义值

javascript - 在数组中,我怎样才能按照我希望打印的方式打印值而不是最后一个值?

node.js - 路线和景观

javascript - Jest 使用 Express 应用程序检测打开句柄

javascript - 创建新子级时如何将parent_id的外键分配给post路由

javascript - WebAudio设置gainNode.gain.value

JavaScript 语法 : var array = []. Push(foo);

css - 将 node_modules 中的 js 和 css 文件包含到静态 html 页面中