Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDb-cb不是一个函数_Mongodb - Fatal编程技术网

MongoDb-cb不是一个函数

MongoDb-cb不是一个函数,mongodb,Mongodb,当我尝试使用节点5.x插入Mongo时,遇到了一点小麻烦。我创建了一个ES6类,该类的属性之一是Mongo集合。我试图在同一ES6类的方法中使用Mongo集合来执行插入。在创建ES6类实例的工厂中构造类时,将设置Mongo连接和集合属性。在工厂内,我通过以下方式创建集合对象的副本: let collection = lodash.cloneDeep(this.collection); 然后通过考试进入ES6课程 let newDoc = new Document (collection);

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

let collection = lodash.cloneDeep(this.collection);
然后通过考试进入ES6课程

let newDoc = new Document (collection);
但是当我调用new时。_create()

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

任何帮助都将不胜感激

G


我也面临着同样的问题,我终于找到了根本原因。我注意到你正在克隆这个收藏。我使用
lodash.merge
做了类似的事情。这似乎有点“损坏”了数据库实例,一些内部处理无法正常工作。如果您删除
cloneDeep
操作,只存储对实例的引用,它可能会工作,这对我来说是可行的

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


希望这能有所帮助。

那么,既然可以以本机方式返回承诺,为什么要将
.insertOne()
包装在承诺中呢?值得思考。谢谢你的建议。我试图直接调用insertOne,但这仍然是一个问题。也许您应该使用本机承诺展示您的新代码,以及调用它的上下文。如果承诺链和调用定义正确,您显然不可能收到基本上是..回调未定义..“的错误消息(因为如果操作正确,将不会有“回调”)。因此,它们不是。这是问题的原因,也是我们在此处无法看到的代码。您找到问题的根源了吗?我遇到了相同的问题
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);
                });
        });
    };