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
:439throw err;^TypeError: db.collection is not a functionat writeData (F:\nodejs\insertData.js:5:22)at F:\nodejs\insertData.js:19:2at 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
:439throw err;^TypeError: collection.insert is not a functionat writeData (F:\nodejs\insertData.js:7:13)at F:\nodejs\insertData.js:19:2at 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
:439throw err;^TypeError: collection.insertOne is not a functionat writeData (F:\nodejs\insertData.js:7:13)at F:\nodejs\insertData.js:19:2at 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 6f6b ... >,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();});
});

TypeError: db.collection is not a function相关推荐

  1. TypeError: db.addUser is not a function : @(shell):1:1 ——mongoDB创建新用户名密码的方法...

    不多说,旧版本使用 db.addUser("root","root") 新版本使用这句会出现这个错误提示 TypeError: db.addUser is no ...

  2. 【备忘录】使用mongodb,报db.collection is not a function

    使用version3版本, const myDb = db.db('YourDatabase') // 加这句 myDb.collection('YourDatabase')...... 或者, mo ...

  3. 安装node-sass运行报错 Module build failed: TypeError: this.getResolve is not a function at Object.loader

    在搭建vue脚手架 或者是在vue项目中,想使用sass的功能, npm install node-sass --save-dev //安装node-sass npm install sass-loa ...

  4. extjs之TypeError: d.read is not a function解决方案

    在创建如下代码时报出此错:TypeError: d.read is not a function 1 Ext.define('shebyxgl_sheb_model', { 2 extend: 'Ex ...

  5. TypeError: Router.use() requires a middleware function but got a Object报错解决

    1.在运行nodejs-mongodb项目时,项目报错 TypeError: Router.use() requires a middleware function but got a Object ...

  6. VUE:解决 [Vue warn]: Error in render: “TypeError: item.slice is not a function“ (取部分数据)

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 双重循环中使用 slice方法,报错: [Vue warn]: Error in render ...

  7. 【Error】Less-loader 版本过高,TypeError: this.getOptions is not a function

    TypeError: this.getOptions is not a function 原因分析 解决方案 原因分析 由于 less-loader 的版本过高,会报出 TypeError: this ...

  8. JQuery中button提交表单报TypeError: elem[type] is not a function jquery

    错误: TypeError: elem[type] is not a function jquery 解决: 出现这种现象的原因是,提交的表单中,有标签的name,有以submit命名的 name中不 ...

  9. Uncaught TypeError: l.push is not a function

    使用新版有如下问题: Uncaught TypeError: l.push is not a function 解决: layui.use([ 'jquery', 'layer', 'element' ...

最新文章

  1. 怎样用QT在windows环境通过编程卸载installShield打包的程序
  2. leetcode算法题--1比特与2比特字符
  3. 删除桌面上淘宝商城,高清电影,精彩小游戏图标
  4. ubuntu16.04安装vim报错 vim : Depends: vim-common (= 2:7.4.052-1ubuntu3.1) but 2:7.4.1689-3ubuntu1.3
  5. 递归过程中语句执行顺序
  6. eclipse中为了format的代码更加好看,少换行,可以设置java、xml、jsp的代码line width。
  7. 机器学习、神经网络中不懂的函数及功能实现
  8. 程序员面试金典——9.8硬币表示
  9. html标签--使用
  10. java新增mysql时 中文出现_Java项目往数据库中插入数据,出现中文乱码
  11. window下开启mysql慢查询和分割慢查询日志
  12. python 计算变量的IV值
  13. matlab中角度,利用 matlab 计算各种角度
  14. 大数据项目实战——基于某招聘网站进行数据采集及数据分析(六)
  15. 全国大学生物联网设计竞赛作品 | 室内消毒机器人-艾盾
  16. NCBI上基因前面有个accession(编号)分别有NC是什么意思
  17. wannier插值能带拟合4
  18. ElasticSearch教程
  19. linux局域网共享目录软链接,网上邻居:LinuxMint局域网SAMBA共享如此简单
  20. 从程序员到项目经理(二十一):谁都需要成就感

热门文章

  1. 杭州计算机职称考试培训,杭州全国职称英语等级考试强化培训班(综合类)
  2. heart(js源码)
  3. 微信小程序——api调用(糗事百科例子)
  4. 分布式存储技术及应用
  5. C++ fstream 创建文件
  6. C语言中声明和定义详解
  7. ArcGIS中将一个shp图层批量分割成单个的shp;shp批量转geojson
  8. linux编程性能,Linux性能评估工具
  9. Python:实现一个Pangram字符串至少包含一次所有字母算法(附完整源码)
  10. Newtonsoft.Json.JsonConvert.SerializeObject()