2015-11-23 17 views
1

連接到本地的MongoDB服務器這不能試圖連接到從項目目錄我得到MONGO分貝時的NodeJS

/用戶/ tadeothompson /文檔/設計工作/壓力/網站/ node_modules /連接 - 蒙戈/ lib/connect-mongo.js:133 throw err; ^ MongoError:無法連接到服務器 在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/designwork/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工作/壓力/網站/ 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/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遍尋。

在此先感謝

+0

請問您能給出完整的堆棧跟蹤。 –

回答

1

我最好的猜測是,您使用的兩個模塊,即MongoClient和貓鼬都試圖連接到端口27017現在,在這場比賽中只有一個會取勝,將鎖定該端口。如果你嘗試綁定到該端口,它會給你一個錯誤,類似於你在上面得到的錯誤。我的建議,不要使用MongoClient。只使用貓鼬。貓鼬提供了許多幫助,而YouTube上的許多視頻教程都使用它。 如果這不幫助我知道。

讓我們準備一些代碼吧。我現在不使用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(); 
     }); 
}); 
+0

感謝您的幫助;我沒有在同一時間運行這兩個模塊。這個問題早在我創建'小測試模塊'之前就已經存在了。我只創建了測試模塊並單獨運行,以查看是否會建立連接,希望能夠排除mongod服務器的問題。 –

+0

所以你的mongod服務器運行正常嗎? 'mongod --dbpath〜/ data/db'打開一個連接? –

+0

確保你的端口27017被釋放。通過運行'netstat -tulpn' –

0

無論是兩個問題之一 - 要麼你調用一個未定義的架構(貓鼬),或者你有需要下一個功能,但未來是不確定的。我多次遇到這個問題,並且記錄了對貓鼬錯誤處理的缺乏。您需要在app.js文件中儘早定義一些錯誤處理。

+0

:(上面添加的代碼)。生病必須回到你的'下一個'問題上 –

1

我發現在另一個堆棧溢出的答案後here

的問題是,會話(或貓鼬之外別的什麼)試圖連接到數據庫之前,貓鼬建立連接。

相關問題