TypeError:无法读取EventEmitter中未定义的属性“ apply”

时间:2019-02-03 18:35:11

标签: javascript node.js express mongoskin dhtmlx-scheduler

我正在使用DHTMLX Scheduler作为页面之一来构建节点js应用程序。但是,无论何时加载页面,应用程序都会崩溃,并且会出现以下错误:

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集群。

app.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应用程序作为独立功能运行时,它可以完美运行。只有当我尝试在我的网站中实现它时,它才会引发此错误。

1 个答案:

答案 0 :(得分:1)

更新:正确答案

此问题与以下内容重复: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操作时,它如何调用db.event.insert()等)