无法使用nodejs连接到本地mongodb服务器

时间:2022-10-10 03:05:49

when trying to connect to mongo db from the project directory i get this

当尝试从项目目录连接到mongo db时,我得到了这个

/Users/tadeothompson/Documents/design work/stressful/site/node_modules/connect-mongo/lib/connect-mongo.js:133 throw err; ^ MongoError: cannot connect to server at Collection.listIndexes (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1712:11) at indexInformation (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/db.js:1531:25) at Db.indexInformation (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/db.js:1498:44) at ensureIndex (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/db.js:1003:8) at Db.ensureIndex (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/db.js:982:44) at ensureIndex (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1772:13) at Collection.ensureIndex (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1760:44) at connectionReady (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/connect-mongo/lib/connect-mongo.js:141:27) at Db.collection (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/mongoose/node_modules/mongodb/lib/db.js:425:20) at initWithNativeDb (/Users/tadeothompson/Documents/design work/stressful/site/node_modules/connect-mongo/lib/connect-mongo.js:207:20) at process._tickCallback (node.js:355:11) at Function.Module.runMain (module.js:503:11) at startup (node.js:129:16) at node.js:814:3

/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:133 throw err; ^ MongoError:无法在indexInformation(/ Users /)处连接到Collection.listIndexes(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / collection.js:1712:11)的服务器Tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1531:25)在Db.indexInformation(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1498:44)在ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1003:8 )在EnsureIndex(/ Users / tadeothompson / Documents / design work)的Db.ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:982:44) /stressful/site/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1772:13)在Collection.ensureIndex(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoo) se / node_modules / mongodb / lib / collection.js:1760:44)在connectionReady(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:141:27 )在Db.collection(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / mongoose / node_modules / mongodb / lib / db.js:425:20)at initWithNativeDb(/ Users / tadeothompson / Documents / design work) /stressful/site/node_modules/connect-mongo/lib/connect-mongo.js:207:20)在function.Module.runMain的process._tickCallback(node.js:355:11)处(module.js:503:11) )在node.js:814:3启动时(node.js:129:16)

managed to connect using a simple app (code below)

设法使用简单的应用程序连接(下面的代码)

*

var MongoClient = require('mongodb').MongoClient;
    // Connect to the db
    MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
      if(!err) {
        console.log("We are connected");
      }
    });

*

the main node file of the app in question code is below:


var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var mongoStore = require('connect-mongo')({session: expressSession});
var mongoose = require('mongoose');
require('./models/users_model.js');
var conn = mongoose.connect('mongodb://localhost:27017/stressfullproject');
var app = express();
app.engine('html', require('ejs')._express); 
app.set('views', './site' + '/views');        
app.set('view engine', 'html');
app.use(bodyParser());
app.use(cookieParser());
app.use(expressSession({
    secret: 'stress',
    cookie: {maxAge: 60*60*1000},
    store: new mongoStore({
        db: mongoose.connection.db,
        collection: 'sessions'
    })
}));
require('./routes/routes')(app);
app.listen(80);

*

my defined schema

我定义的架构

 *var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
var UserSchema = new Schema({
    username: { type: String, unique: true },
    email: String,
    hashed_password: String
})
mongoose.model('User', UserSchema)*

because i can connect with the other app, im thinking its an issue with one of my modules? ive searched all over.

因为我可以与其他应用程序连接,我认为它是我的一个模块的问题?我到处搜索。

thanks in advance

提前致谢

3 个解决方案

#1


1  

My best guess is that you are using two modules namely MongoClient and mongoose both are trying to connect to port 27017. Now in this race only one will win and will lock that port. If you try and bind to that port it will give you an error, similar to the one you are getting above. My suggestion, don't use MongoClient. use only mongoose. There is a lot of help available for mongoose and many video tutorials on youtube use it. If that doesn't help let me know.

我最好的猜测是你使用两个模块,即MongoClient和mongoose都试图连接到端口27017.现在在这场比赛中只有一个会赢,并将锁定该端口。如果您尝试绑定到该端口,它将给您一个错误,类似于您上面提到的错误。我的建议,不要使用MongoClient。只使用猫鼬。 mongoose有很多帮助,youtube上的许多视频教程都使用它。如果这没有帮助让我知道。

Let's prep you up with some code shall we. I don't use MongoClient now, but when I used to I wrote this code, see of it works. If it doesn't please paste the stacktrace.

让我们为你准备一些代码。我现在不使用MongoClient,但是当我以前编写这段代码时,看到它的工作原理。如果没有,请粘贴堆栈跟踪。

var MongoClient=require('mongodb').MongoClient,

server=require('mongodb').Server;

var mongoclient=new MongoClient(new server('localhost',27017));

mongoclient.connect('mongodb://localhost:27017/course',function(err,db)
{
    if(err) throw err;
    //var db=mongoclient.db('course');
    var query={'grade':100};

    db.collection('grades').findOne(query,function(err,doc)
        {
            if(err) throw err;
            console.dir(doc);
            db.close();
        });
});

#2


1  

i found the answer in another stack overflow post here

我在这里找到了另一个堆栈溢出帖子的答案

the problem was that the session (or something else outside of mongoose) was trying to connect to the database BEFORE mongoose established a connection.

问题是,在mongoose建立连接之前,会话(或mongoose之外的其他东西)试图连接到数据库。

#3


0  

Either one of two issues - either you are calling an undefined schema (mongoose) or you have a function requiring next but next is undefined. I've run into this problem many times and it's documented lack of error handling with mongoose. You need to define some error handling early in your app.js file.

两个问题中的任何一个 - 要么是调用未定义的模式(mongoose),要么是有一个需要next但next的函数是未定义的。我已多次遇到这个问题,并且记录了mongoose缺少错误处理。您需要在app.js文件的早期定义一些错误处理。

#1


1  

My best guess is that you are using two modules namely MongoClient and mongoose both are trying to connect to port 27017. Now in this race only one will win and will lock that port. If you try and bind to that port it will give you an error, similar to the one you are getting above. My suggestion, don't use MongoClient. use only mongoose. There is a lot of help available for mongoose and many video tutorials on youtube use it. If that doesn't help let me know.

我最好的猜测是你使用两个模块,即MongoClient和mongoose都试图连接到端口27017.现在在这场比赛中只有一个会赢,并将锁定该端口。如果您尝试绑定到该端口,它将给您一个错误,类似于您上面提到的错误。我的建议,不要使用MongoClient。只使用猫鼬。 mongoose有很多帮助,youtube上的许多视频教程都使用它。如果这没有帮助让我知道。

Let's prep you up with some code shall we. I don't use MongoClient now, but when I used to I wrote this code, see of it works. If it doesn't please paste the stacktrace.

让我们为你准备一些代码。我现在不使用MongoClient,但是当我以前编写这段代码时,看到它的工作原理。如果没有,请粘贴堆栈跟踪。

var MongoClient=require('mongodb').MongoClient,

server=require('mongodb').Server;

var mongoclient=new MongoClient(new server('localhost',27017));

mongoclient.connect('mongodb://localhost:27017/course',function(err,db)
{
    if(err) throw err;
    //var db=mongoclient.db('course');
    var query={'grade':100};

    db.collection('grades').findOne(query,function(err,doc)
        {
            if(err) throw err;
            console.dir(doc);
            db.close();
        });
});

#2


1  

i found the answer in another stack overflow post here

我在这里找到了另一个堆栈溢出帖子的答案

the problem was that the session (or something else outside of mongoose) was trying to connect to the database BEFORE mongoose established a connection.

问题是,在mongoose建立连接之前,会话(或mongoose之外的其他东西)试图连接到数据库。

#3


0  

Either one of two issues - either you are calling an undefined schema (mongoose) or you have a function requiring next but next is undefined. I've run into this problem many times and it's documented lack of error handling with mongoose. You need to define some error handling early in your app.js file.

两个问题中的任何一个 - 要么是调用未定义的模式(mongoose),要么是有一个需要next但next的函数是未定义的。我已多次遇到这个问题,并且记录了mongoose缺少错误处理。您需要在app.js文件的早期定义一些错误处理。