TypeError: db.collection is not a function 2022-05-16 08:11 74阅读 0赞 1、错误描述 F:\nodejs>node insertData.js (node:10028) DeprecationWarning: current URL string parser is deprecated, and wi ll be removed in a future version. To use the new parser, pass option { useNewUr lParser: true } to MongoClient.connect. ���ӳɹ��� F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_client_ops.js :439 throw err; ^ TypeError: db.collection is not a function at writeData (F:\nodejs\insertData.js:5:22) at F:\nodejs\insertData.js:19:2 at result (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils.js:414:17 ) at executeCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils. js:406:9) at err (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_c lient_ops.js:285:5) at connectCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operat ions\mongo_client_ops.js:240:5) at process.nextTick (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\opera tions\mongo_client_ops.js:436:7) at process._tickCallback (internal/process/next_tick.js:61:11) F:\nodejs>node insertData.js (node:4900) DeprecationWarning: current URL string parser is deprecated, and wil l be removed in a future version. To use the new parser, pass option { useNewUrl Parser: true } to MongoClient.connect. ���ӳɹ��� F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_client_ops.js :439 throw err; ^ TypeError: collection.insert is not a function at writeData (F:\nodejs\insertData.js:7:13) at F:\nodejs\insertData.js:19:2 at result (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils.js:414:17 ) at executeCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils. js:406:9) at err (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_c lient_ops.js:285:5) at connectCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operat ions\mongo_client_ops.js:240:5) at process.nextTick (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\opera tions\mongo_client_ops.js:436:7) at process._tickCallback (internal/process/next_tick.js:61:11) F:\nodejs>node insertData.js (node:9572) DeprecationWarning: current URL string parser is deprecated, and wil l be removed in a future version. To use the new parser, pass option { useNewUrl Parser: true } to MongoClient.connect. ���ӳɹ��� F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_client_ops.js :439 throw err; ^ TypeError: collection.insertOne is not a function at writeData (F:\nodejs\insertData.js:7:13) at F:\nodejs\insertData.js:19:2 at result (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils.js:414:17 ) at executeCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\utils. js:406:9) at err (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operations\mongo_c lient_ops.js:285:5) at connectCallback (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\operat ions\mongo_client_ops.js:240:5) at process.nextTick (F:\nodejs\node_modules\_mongodb@3.1.1@mongodb\lib\opera tions\mongo_client_ops.js:436:7) at process._tickCallback (internal/process/next_tick.js:61:11) F:\nodejs>node insertData.js (node:10664) DeprecationWarning: current URL string parser is deprecated, and wi ll be removed in a future version. To use the new parser, pass option { useNewUr lParser: true } to MongoClient.connect. ���ӳɹ��� ����MongoError: doc parameter must be an object ^C F:\nodejs>node insertData.js (node:10308) DeprecationWarning: current URL string parser is deprecated, and wi ll be removed in a future version. To use the new parser, pass option { useNewUr lParser: true } to MongoClient.connect. ���ӳɹ��� CommandResult { result: { n: 1, ok: 1 }, connection: Connection { _events: { error: [Function], close: [Function], timeout: [Function], parseError: [Function] }, _eventsCount: 4, _maxListeners: undefined, options: { host: 'localhost', port: 27017, size: 5, minSize: 0, connectionTimeout: 30000, socketTimeout: 360000, keepAlive: true, keepAliveInitialDelay: 300000, noDelay: true, ssl: false, checkServerIdentity: true, ca: null, crl: null, cert: null, key: null, passPhrase: null, rejectUnauthorized: false, promoteLongs: true, promoteValues: true, promoteBuffers: false, reconnect: true, reconnectInterval: 1000, reconnectTries: 30, domainsEnabled: false, disconnectHandler: [Store], cursorFactory: [Function], emitError: true, monitorCommands: false, socketOptions: {}, promiseLibrary: [Function: Promise], clientInfo: [Object], read_preference_tags: null, readPreference: [ReadPreference], dbName: 'student', servers: [Array], server_options: [Object], db_options: [Object], rs_options: [Object], mongos_options: [Object], socketTimeoutMS: 360000, connectTimeoutMS: 30000, bson: BSON {} }, id: 0, logger: Logger { className: 'Connection' }, bson: BSON {}, tag: undefined, messageHandler: [Function], maxBsonMessageSize: 67108864, port: 27017, host: 'localhost', family: undefined, keepAlive: true, keepAliveInitialDelay: 300000, noDelay: true, connectionTimeout: 30000, socketTimeout: 360000, destroyed: false, domainSocket: false, singleBufferSerializtion: true, serializationFunction: 'toBinUnified', ca: null, crl: null, cert: null, key: null, passphrase: null, ciphers: null, ecdhCurve: null, ssl: false, rejectUnauthorized: false, checkServerIdentity: true, responseOptions: { promoteLongs: true, promoteValues: true, promoteBuffers: false }, flushing: false, queue: [], connection: Socket { connecting: false, _hadError: false, _handle: [TCP], _parent: null, _host: 'localhost', _readableState: [ReadableState], readable: true, _events: [Object], _eventsCount: 5, _maxListeners: undefined, _writableState: [WritableState], writable: true, allowHalfOpen: false, _sockname: null, _pendingData: null, _pendingEncoding: '', server: null, _server: null, timeout: 360000, [Symbol(asyncId)]: 15, [Symbol(lastWriteQueueSize)]: 0, [Symbol(timeout)]: [Timeout], [Symbol(kBytesRead)]: 0, [Symbol(kBytesWritten)]: 0 }, writeStream: null, hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c', workItems: [], buffer: null, sizeOfMessage: 0, bytesRead: 0, stubBuffer: null }, message: Response { parsed: true, raw: <Buffer 3c 00 00 00 f0 02 00 00 02 00 00 00 01 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 18 00 00 00 10 6e 00 01 00 00 00 01 6f 6b ... >, data: <Buffer 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 18 00 00 00 10 6e 00 01 00 00 00 01 6f 6b 00 00 00 00 00 00 00 f0 3f 00>, bson: BSON {}, opts: { promoteLongs: true, promoteValues: true, promoteBuffers: false }, length: 60, requestId: 752, responseTo: 2, opCode: 1, fromCompressed: undefined, responseFlags: 8, cursorId: Long { _bsontype: 'Long', low_: 0, high_: 0 }, startingFrom: 0, numberReturned: 1, documents: [ [Object] ], cursorNotFound: false, queryFailure: false, shardConfigStale: false, awaitCapable: true, promoteLongs: true, promoteValues: true, promoteBuffers: false, index: 44, hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c' }, ops: [ { name: 'zhangsan', age: '30', _id: 5b70f4bea602b02844490a15 } ], insertedCount: 1, insertedId: 5b70f4bea602b02844490a15 } F:\nodejs> 2、错误原因 在使用Node.js操作MongoDB数据库,想向里面插入数据记录,写了一个JavaScript文件,调用了collection函数,但是这个旧版本的方法,新版本的方法不是这个,换成db()。 3、解决办法 var MongoClient = require('mongodb').MongoClient; var DB_URL = 'mongodb://localhost:27017/student'; var writeData = function(db,callback){ var dbo = db.db('t_sales'); var data = {'name':'zhangsan','age':'30'}; dbo.collection('t_sales').insertOne(data,function(error,result){ if(error){ console.log('错误:'+error); return; }; callback(result); }); } MongoClient.connect(DB_URL,function(error,db){ console.log('连接成功!'); writeData(db,function(result){ console.log(result); db.close(); }); }); 文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。
相关 Syntax Error: TypeError: this.getOptions is not a function ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ub 港控/mmm°/ 2021年07月26日 09:43/ 0 赞/ 351 阅读
相关 Uncaught TypeError: form.render is not a function 引言:一直报这个错误,刚开始不知道哪里的问题,后来才明白原来是,引入的layui.js跟引入jquery的包产生了冲突。 把引入的js包注释掉就可以了,不会产生冲突了。 < 我会带着你远行/ 2021年09月23日 07:14/ 0 赞/ 110 阅读
相关 "TypeError: handler.call is not a function"问题 进入组件页面时,vue报错:Error in mounted hook: "TypeError: handler.call is not a function", ![157 梦里梦外;/ 2021年11月27日 06:48/ 0 赞/ 160 阅读
相关 TypeError: instance.render is not a function 1、错误描述 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly95b3VoYWlkb25nLmJs 旧城等待,/ 2022年01月28日 23:39/ 0 赞/ 99 阅读
相关 TypeError: "_vm.onetopic is not a function" 解决方案 1、方法 onetopic要放在methods:\{\}里面 2、看看自己是不是多个\},导致方法在methods:\{\}外面了 ------------ 浅浅的花香味﹌/ 2022年02月01日 11:15/ 1 赞/ 141 阅读
相关 TypeError: db.collection is not a function 1、错误描述 F:\nodejs>node insertData.js (node:10028) DeprecationWarning: current UR 比眉伴天荒/ 2022年05月16日 08:11/ 0 赞/ 75 阅读
相关 TypeError:(0 ,jsdom.jsdom) is not a function 在使用`Enzyme`测试容器组件时,为了能使用`mount`方法递归渲染出所有`DOM`,不得不使用`JSDOM`在`Node.js中`模拟一个类似浏览器的环境。于是有了下面 朱雀/ 2022年05月25日 19:57/ 0 赞/ 83 阅读
相关 Uncaught TypeError: c(...).off is not a function(… layer.open({ title: 'test', content: 'hello layer' }) 运行上面的代码,能弹出对话框,但按确定对话框不消失. 控制 喜欢ヅ旅行/ 2022年05月29日 21:05/ 0 赞/ 68 阅读
相关 Uncaught TypeError: XXX.getTime is not a function 因为变量只是字符串,无法调用日期函数。 你应该解析它们以获得一个`Date`对象,对于该格式我总是使用以下函数: // parse a date in yyyy-m 淡淡的烟草味﹌/ 2022年09月27日 17:16/ 0 赞/ 80 阅读
相关 Vue TypeError: this.$confirm is not a function 错误 在使用element ui,采用局部引入时候,报错`TypeError: this.$confirm is not a function`。 ![在这里插入图片 Dear 丶/ 2022年12月23日 14:24/ 0 赞/ 55 阅读
还没有评论,来说两句吧...