TypeError: db.collection is not a function
2018-01-08 17:30
776 查看
nodejs调用mongodb驱动写入数据异常
nodejs 版本:v8.9.4mongodb 版本:3.4.7
问题描述:
使用nodejs,连接mongodb,数据写入时提示如下错误:$ node insertdb.js 连接成功![object Object] e:\test\node_modules\_mongodb@3.0.1@mongodb\lib\mongo_client.js:797 throw err; ^ TypeError: db.collection is not a function at insertData (e:\test\insertdb.js:9:25) at e:\test\insertdb.js:25:5 at args.push (e:\test\node_modules\_mongodb@3.0.1@mongodb\lib\utils.js:404:72) at e:\test\node_modules\_mongodb@3.0.1@mongodb\lib\mongo_client.js:255:5 at connectCallback (e:\test\node_modules\_mongodb@3.0.1@mongodb\lib\mongo_client.js:933:5) at e:\test\node_modules\_mongodb@3.0.1@mongodb\lib\mongo_client.js:794:11 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9)
问题分析:
1、根据错误提示:TypeError: collection is not a function说明函数:
collection未找到。
2、从错误堆栈中,定位到node的lib目录,结构如下:
3、错误堆栈中,仅定位到
mongo_client.js,还没有到数据库一级就出现异常了。
4、打开文件
collection.js,搜索
collection方法,找到如下代码示例片段,原来mongodb 3.x版本的库文件与2.x版本的不一样,按照示例修改就可以了:
示例代码:
// mongodb // 插入数据 var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:30000/runoob'; var dbName = 'runnoob'; var insertData = function(db, callback) { //连接到表 site var collection = db.collection('site'); //插入数据 var data = [{"name":"菜鸟教程","url":"www.runoob.com"},{"name":"菜鸟工具","url":"c.runoob.com"}]; collection.insert(data, function(err, result) { if(err) { console.log('Error:'+ err); return; } callback(result); }); } /*连接方式1:错误 MongoClient.connect(url, function(err, db) { console.log("连接成功!" + db); insertData(db, function(result) { console.log(result); db.close(); }); }); */ /* 连接方式2:正确*/ MongoClient.connect(url, function(err, client) { console.log("连接成功!" + client); var db = client.db(dbName) insertData(db, function(result) { console.log(result); client.close(); }); });
参考
Node.js 连接 MongoDBhttp://www.runoob.com/nodejs/nodejs-mongodb.html
mongodb官网帮助:
http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/
相关文章推荐
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- TypeError: db.addUser is not a function : @(shell):1:1 ——mongoDB创建新用户名密码的方法
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- mongodb 添加用户报错TypeError:db.addUser is not a function
- E QUERY [thread1] TypeError: db.foo.batchInsert is not a function :
- jQuery.Deferred exception: a.indexOf is not a function TypeError: a.indexOf is not a function
- typeError: $(...).ajaxSubmit is not a function
- 【ReactJS 初体验】Module build failed: TypeError: fileSystem.statSync is not a function
- python TypeError: 'builtin_function_or_method' object is not iterable keys
- ExtJS 5.1 - tabpanel setActiveTab: Uncaught TypeError: c.isFocusable is not a function
- Uncaught TypeError: undefined is not a function
- vue项目使用手机浏览器访问时报错:Uncaught TypeError: Object.assign is not a function
- Uncaught TypeError: window.showModalDialog is not a function chrome 谷歌浏览器
- Uncaught TypeError: object is not a function
- python TypeError: 'builtin_function_or_method' object is not iterable keys
- Uncaught TypeError: sum is not a function
- TypeError: url.indexOf is not a function ,使用jquery load()出现错误
- uploadify Uncaught TypeError: $(...).uploadify is not a function