Greg Svitak
Greg Svitak

Reputation: 485

MongoDb - cb is not a function

I am having a little trouble when I am trying to insert into Mongo with node 5.x. I have created an ES6 class that has the Mongo collection as one of the properties of the class. I trying to use the Mongo collection within a method on the same ES6 class to do the insert. The Mongo connection and the collection property is set when the class is constructed in a factory that creates the instance of the ES6 class. Within the factory I am creating a copy of the collection object by:

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

and then passing into the ES6 class by

let newDoc = new Document (collection);

However when I called new._create()

I receive the stack trace below. I have also included the _create method for reference.

Any help would be appreciated,

G

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);
                });
        });
    };

Upvotes: 1

Views: 1085

Answers (1)

Danielo515
Danielo515

Reputation: 7151

I was facing the same problem, and I finally found the root cause. I noticed that you are cloning the collection. I was doing something similar using lodash.merge. This seems to somewhat "corrupt" the database instance, and some internals handlings does not work properly. If you remove the cloneDeep operation and just store the reference to the instance it will probably work, it did for me.

This is somewhat inconvenient, specially in cases like mine, where I don't want to clone the object at all, but just merge the properties in a safe manner. Hopefully for you the workaround is quite simple, for me it was a bit complicated to fix.

Hope this helps.

Upvotes: 1

Related Questions