MongoDb - cb不是一个函数

时间:2016-02-15 06:31:11

标签: mongodb

当我尝试使用节点5.x插入Mongo时,我遇到了一些麻烦。我创建了一个ES6类,它将Mongo集合作为该类的一个属性。我试图在同一个ES6类的方法中使用Mongo集合来进行插入。当在创建ES6类实例的工厂中构造类时,将设置Mongo连接和collection属性。在工厂内我通过以下方式创建集合对象的副本:

let collection = lodash.cloneDeep(this.collection);

然后通过

进入ES6课程
let newDoc = new Document (collection);

然而,当我打电话给new._create()

我收到下面的堆栈跟踪。我还提供了_create方法供参考。

任何帮助将不胜感激,

TypeError: cb is not a function
            at afterWrite (_stream_writable.js:346:3)
            at onwrite (_stream_writable.js:337:7)
            at WritableState.onwrite (_stream_writable.js:89:5)
            at Socket._writeGeneric (net.js:684:5)
            at Socket._write (net.js:694:8)
            at doWrite (_stream_writable.js:292:12)
            at writeOrBuffer (_stream_writable.js:278:5)
            at Socket.Writable.write (_stream_writable.js:207:11)
            at Socket.write (net.js:618:40)
            at Connection.write (/home/user/git/DBStore/node_modules/mongodb-core/lib/connection/connection.js:428:53)
            at _execute (/home/user/git/DBStore/node_modules/mongodb-core/lib/connection/pool.js:411:24)
            at Pool.write (/home/user/git/DBStore/node_modules/mongodb-core/lib/connection/pool.js:454:17)
            at executeSingleOperation (/home/user/git/DBStore/node_modules/mongodb-core/lib/topologies/server.js:955:19)
            at Server.command (/home/user/git/DBStore/node_modules/mongodb-core/lib/topologies/server.js:1038:3)
            at executeWrite (/home/user/git/DBStore/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:60:12)
            at WireProtocol.insert (/home/user/git/DBStore/node_modules/mongodb-core/lib/wireprotocol/3_2_support.js:68:3)
            at Server.insert (/home/user/git/DBStore/node_modules/mongodb-core/lib/topologies/server.js:1066:37)
            at Server.insert (/home/user/git/DBStore/node_modules/mongodb/lib/server.js:325:17)
            at insertDocuments (/home/user/git/DBStore/node_modules/mongodb/lib/collection.js:680:19)
            at insertOne (/home/user/git/DBStore/node_modules/mongodb/lib/collection.js:402:3)
            at Collection.insertOne (/home/user/git/DBStore/node_modules/mongodb/lib/collection.js:390:44)
            at Document._create (/home/user/git/DBStore/lib/document.js:222:20)
            at DescriptorModifier.updateDescriptor (/home/user/git/DBStore/lib/descriptor-modifier.js:28:38)
            at /home/user/git/DBStore/test/document-store-core-tests.js:240:47

_create(options) {

        let collection = this[_mongoCollectionSymbol];
        let attrs = this[_attributesSymbol];

        this._setFieldValue('updated_at', new Date());

        return new Promise(function (resolve, reject) {
            collection.insertOne({ x:1 },
                function (err, result) {
                    if (err) {
                        reject(err);
                        return;
                    }
                    resolve(result);
                });
        });
    };

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我终于找到了根本原因。我注意到你正在克隆这个集合。我正在使用lodash.merge做类似的事情。这似乎有点“破坏”数据库实例,并且一些内部处理不能正常工作。如果你删除了cloneDeep操作并且只是存储了对实例的引用它可能会起作用,它就是为我做的。

这有点不方便,特别是在像我这样的情况下,我根本不想克隆对象,只是以安全的方式合并属性。希望对你来说解决方法非常简单,对我来说修复起来有点复杂。

希望这有帮助。