node.js连接MongoDB数据成功之后,抛出错误,db.collection is not a function
2018-01-03 17:01
896 查看
node.js代码,显示连接数据库成功,却报错db.collection is not a function
代码确定是没错的,连数据库成功,那么问题就出在插入操作上,但插入的代码检查了也是没问题的。最后怀疑是模块出了问题。版本问题。
百度得知:这个错误是出在mongodb的库中,在nodejs里的写法和命令行中的写法不一样,3.0的api已经更新和以前的版本不不一样,我们在npm中没指定版本号的安装就默认安装的是3.0版本。
可以参考3.0的api文档:http://mongodb.github.io/node-mongodb-native/3.0/api/
下面是我在网上收集的一个3.0的方法,能不能用我没试过,因为我用的是回滚版本的办法用老版本来操作。
// 3.0 以上 高版本语法
const express = require('express');
const router = express.Router();
const mongoClient = require('mongodb').MongoClient;
const assert = require('assert');
mongoClient.connect("数据库地址", function(err, client) { assert.equal(null, err);
// admin 为 数据库名称;
const db = client.db("admin");
// PASS 为 数据库里面的某一个集合;
const pass = db.collection('PASS');
pass.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log(docs);
});
});
module.exports = router;
没准备好使用新版本在这里只需要把node_modules中mongodb的版本换为2.3.33vision即可解决;
1、在项目中找到package.json文件,这个文件中记录着我们项目的信息及引用的模块,我们找到mongodb将其版本2.3.33,就像我图片中那样,我原来是3.0.33版本的,图片是我改正后的版本
2、打开命令行: npm install,更新一下,我们就可以使用2.3.33版本的mongodb模块了
这时候在运行一下代码操作就成功了,成功解决问题。
/** * Created by 村中长相较好的男子 on 2018/1/3. */ //引入mongodb模块,获得客户端对象 var MongoClient = require('mongodb').MongoClient; //连接字符串 var DB_CONN_STR = 'mongodb://localhost:27017/gomall'; //定义函数表达式,用于操作数据库并返回结果 var insertData = function(db, callback) { //获得指定的集合 var collection = db.collection('users'); //插入数据 var data = [{_id:7,"name":'rose',"age":21},{_id:8,"name":'mark',"age":22}]; collection.insert(data, function(err, result) { //如果存在错误 if(err) { console.log('Error:'+ err); return; } //调用传入的回调方法,将操作结果返回 callback(result); }); }; //使用客户端连接数据,并指定完成时的回调方法 MongoClient.connect(DB_CONN_STR, function(err, db) { console.log("连接成功!"); //执行插入数据操作,调用自定义方法 insertData(db, function(result) { //显示结果 console.log(result); //关闭数据库 db.close(); }); });
代码确定是没错的,连数据库成功,那么问题就出在插入操作上,但插入的代码检查了也是没问题的。最后怀疑是模块出了问题。版本问题。
百度得知:这个错误是出在mongodb的库中,在nodejs里的写法和命令行中的写法不一样,3.0的api已经更新和以前的版本不不一样,我们在npm中没指定版本号的安装就默认安装的是3.0版本。
可以参考3.0的api文档:http://mongodb.github.io/node-mongodb-native/3.0/api/
下面是我在网上收集的一个3.0的方法,能不能用我没试过,因为我用的是回滚版本的办法用老版本来操作。
// 3.0 以上 高版本语法
const express = require('express');
const router = express.Router();
const mongoClient = require('mongodb').MongoClient;
const assert = require('assert');
mongoClient.connect("数据库地址", function(err, client) { assert.equal(null, err);
// admin 为 数据库名称;
const db = client.db("admin");
// PASS 为 数据库里面的某一个集合;
const pass = db.collection('PASS');
pass.find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log(docs);
});
});
module.exports = router;
没准备好使用新版本在这里只需要把node_modules中mongodb的版本换为2.3.33vision即可解决;
1、在项目中找到package.json文件,这个文件中记录着我们项目的信息及引用的模块,我们找到mongodb将其版本2.3.33,就像我图片中那样,我原来是3.0.33版本的,图片是我改正后的版本
2、打开命令行: npm install,更新一下,我们就可以使用2.3.33版本的mongodb模块了
这时候在运行一下代码操作就成功了,成功解决问题。
相关文章推荐
- node.js连接MongoDB数据库成功之后报错:db.collection is not a function
- Node.js 连接Mongodb错误:db.listCollections is not a function
- NodeJs连接mongodb数据库成功之后报db.collection is not a function错误-已解决
- jquery TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [jquery.js:1904]错误原因
- node.js开发错误——TypeError: req.flash is not a function
- 使用JAVA向MongoDB中插入数据时报错The method insert(DBObject[]) in the type DBCollection is not applicable
- js提交表单错误:document.form.submit() is not a function
- js错误:Uncaught TypeError: accept is not a function at HTMLInputElement.onclick
- js提交表单错误:document.form.submit() is not a function
- TypeError: db.addUser is not a function : @(shell):1:1 ——mongoDB创建新用户名密码的方法
- TypeError: db.collection is not a function
- js提交表单错误:document.form.submit() is not a function
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- js提交表单错误:document.form.submit() is not a function
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- JS中出现“$(...).dropdown is not a function”错误的解决
- js错误Uncaught TypeError: $(...).datagrid is not a function自己写的并没有错但是一直报这个错(详解)
- mongodb 添加用户报错TypeError:db.addUser is not a function
- mongodb 添加用户报错TypeError:db.addUser is not a function (mongodb3.4.1)
- jquery-ui.min.js:5 Uncaught TypeError: b.nodeName.toLowerCase is not a function