无法通过nodejs连接到本地mongodb服务器

当试图从项目目录连接到mongo db我得到这个

/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:133 throw err; ^ MongoError:无法连接到服务器在Collection.listIndexes(/用户/ tadeothompson /文件/devise工作/压力/网站/节点模块/ mongoose / node_modules / mongodb /库/ collection.js:1712:11)在indexInformation(/用户/在Db.indexInformation(/ Users / tadeothompson / Documents / design work / stressful / site / node_modules /文件/devise工作/文件/devise工作/压缩/ site / node_modules / mongoose / node_modules / mongodb / lib / db.js:1531:25) 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 )在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 (/ Users / tadeothompson / Documents / design work / stressful / site / node_modules / connect-mongo / lib / connect-mongo.js:141:27)/etc/mongodb/lib/collection.js:1760:44) )在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 )在启动(node.js:129:16)在node.js:814:3

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

* 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); 

*

我定义的模式

  *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)* 

因为我可以与其他应用程序连接,即时通讯考虑与我的模块之一的问题? ive遍寻。

提前致谢

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

我们来准备一下你的代码吧。 我现在不使用MongoClient,但是当我以前写这个代码的时候,看到它的作用。 如果它不请粘贴stacktrace。

 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(); }); }); 

我在这里find了另一个堆栈溢出的答案

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

两个问题中的任何一个 – 要么调用一个未定义的模式(mongoose),要么你有一个函数需要next,但next是未定义的。 我已经遇到过这个问题多次,它logging了mongoose缺乏error handling。 您需要在app.js文件中尽早定义一些error handling。