错误连接到Nodejs上的mongodb(error connecting to mongodb on Nodejs)

我正尝试从节点连接到mongodb,并且出现错误

node_modules \ mongodb \ lib \ mongo_client.js:458 throw err ^

ReferenceError:连接未定义

我使用的是mongodb模块版本2.0.48

我正在尝试运行一个简单的测试代码

(function (dbase) { var mdb = require('mongodb'); var mongoUrl = "mongodb://localhost:27017/theBoard"; var connection; dbase.dbConnection = function (next) { if (connection) { next(null, connection); } else { mdb.MongoClient.connect(mongoUrl, function(err, db) { if (err) { next(err, null); } else { console.log("connected"); connection = { db: db , notes: db.collection("notes")}; next(null, connection); } }); } }

有人可以帮我理解这个问题。

- -附加信息

数据模块 -

(function (data) { var mdb = require('./db.js'); data.GetCategory = function() { mdb.dbConnection(function(err, db) { if (err) console.log("Error connecting to mango"); if (connect) { db.notes.count(function(err, count) { if (err) console.log("Failed to retreive collection"); else console.log("Count - "+count); }); console.log("Connected"); } }); }})(module.exports);

db.js

(function (dbase) { var mdb = require('mongodb'); var mongoUrl = "mongodb://localhost:27017/theBoard"; var connection; dbase.dbConnection = function (next) { if (connection) { next(null, connection); } else { mdb.MongoClient.connect(mongoUrl, function(err, db) { if (err) { next(err, null); } else { console.log("connected"); connection = { db: db , notes: db.collection("notes") }; next(null, connection); } }); } } })(module.exports);

控制器 -

(function (controller) { var data = require('.././data'); controller.init = function (app) { app.get("/", handleRequest); } var handleRequest = function (req, res) { data.GetCategory(); var a = {}; a.send = "Mamma is coming home"; res.send(a); } })(module.exports);

I am trying to connect to mongodb from node and I am getting below error

node_modules\mongodb\lib\mongo_client.js:458 throw err ^

ReferenceError: connect is not defined

I am using the mongodb module version 2.0.48

I am trying to run a simple test code

(function (dbase) { var mdb = require('mongodb'); var mongoUrl = "mongodb://localhost:27017/theBoard"; var connection; dbase.dbConnection = function (next) { if (connection) { next(null, connection); } else { mdb.MongoClient.connect(mongoUrl, function(err, db) { if (err) { next(err, null); } else { console.log("connected"); connection = { db: db , notes: db.collection("notes")}; next(null, connection); } }); } }

Can someone please help me understand this issue.

---Additional information

data module -

(function (data) { var mdb = require('./db.js'); data.GetCategory = function() { mdb.dbConnection(function(err, db) { if (err) console.log("Error connecting to mango"); if (connect) { db.notes.count(function(err, count) { if (err) console.log("Failed to retreive collection"); else console.log("Count - "+count); }); console.log("Connected"); } }); }})(module.exports);

db.js

(function (dbase) { var mdb = require('mongodb'); var mongoUrl = "mongodb://localhost:27017/theBoard"; var connection; dbase.dbConnection = function (next) { if (connection) { next(null, connection); } else { mdb.MongoClient.connect(mongoUrl, function(err, db) { if (err) { next(err, null); } else { console.log("connected"); connection = { db: db , notes: db.collection("notes") }; next(null, connection); } }); } } })(module.exports);

Controller -

(function (controller) { var data = require('.././data'); controller.init = function (app) { app.get("/", handleRequest); } var handleRequest = function (req, res) { data.GetCategory(); var a = {}; a.send = "Mamma is coming home"; res.send(a); } })(module.exports);

最满意答案

以防由于错误的编码习惯而导致某些人遇到类似问题,即使它用于测试目的是永远不会有与API中的函数名称相同的函数名称。 在db.js中,我有一个未定义的变量,名为connect,它在访问时出现错误和错误,并且由于通过称为“connect”的API函数调用了错误,API导致错误导致我认为API函数有问题

Just in case some one runs into an issue like this due to bad coding practice even if it for test purpose is to never have function names which are the same as the function names in the API. In db.js I had an undefined variable named connect which trowing and error when it was accessed and since it was called through the API function called "connect" the error was thrown by the API leading me to believe that the API function had an issue

更多推荐