简体   繁体   English

在Node.js上连接到mongodb时出错

[英]error connecting to mongodb on Nodejs

I am trying to connect to mongodb from node and I am getting below error 我正在尝试从节点连接到mongodb,并且出现以下错误

node_modules\\mongodb\\lib\\mongo_client.js:458 throw err ^ node_modules \\ mongodb \\ lib \\ mongo_client.js:458抛出错误^

ReferenceError: connect is not defined ReferenceError:未定义连接

I am using the mongodb module version 2.0.48 我正在使用mongodb模块版本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 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);

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. 万一有人由于不良的编码习惯而遇到这样的问题,即使出于测试目的,也不要让函数名与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 在db.js中,我有一个名为connect的未定义变量,在访问该变量时会引起麻烦和错误,并且由于它是通过称为“ connect”的API函数调用的,因此该API引发了错误,这使我相信该API函数存在问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM