当使用MongoClientv3.0时,db.Collection不是函数我一直在努力W3学校教程关于MongoDB的NodeJS。当我试图在NodeJS环境中实现这个示例并使用Ajax调用该函数时,我得到了以下错误:TypeError: db.collection is not a function
at c:\Users\user\Desktop\Web Project\WebService.JS:79:14
at args.push (c:\Users\user\node_modules\mongodb\lib\utils.js:431:72)
at c:\Users\user\node_modules\mongodb\lib\mongo_client.js:254:5
at connectCallback (c:\Users\user\node_modules\mongodb\lib\mongo_client.js:933:5)
at c:\Users\user\node_modules\mongodb\lib\mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)请在下面找到我的实现代码:var MongoClient = require('mongodb').MongoClient;var url = "mongodb://localhost:27017/mytestingdb";MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("customers").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});});请注意,每当执行命中时,错误就会发生:db.collection("customers").findOne({}, function(err, result) {}另外,请注意(如果这很重要),我已经为节点JS安装了最新的MongoDB包(NPM安装MongoDB),而MongoDB版本是MongoDBEnterprise3.4.4,MongoDBNode.js驱动程序v3.0.0-rc0。
3 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
MongoClient.connect()
db
client
db(dbname)
db
const MongoClient = require('mongodb').MongoClient;const assert = require('assert'); // Connection URLconst url = 'mongodb://localhost:27017'; // Database Nameconst dbName = 'myproject'; // Use connect method to connect to the serverMongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); client.close();});
- 3 回答
- 0 关注
- 661 浏览
添加回答
举报
0/150
提交
取消