2017-06-14 76 views
0

我有一個基於express.js的node.js應用程序,它可以在本地主機上正常工作,它也可以連接Mlab數據庫本地主機:Heroku Node.js(express.js)應用程序本地工作,但在使用MongoDB時在heroku上失敗

[email protected]:~/Work/smr_pjt_loct_dr$ NODE_ENV=production MONGODB_URI=mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd nodemon start 
[nodemon] 1.11.0 
[nodemon] to restart at any time, enter `rs` 
[nodemon] watching: *.* 
[nodemon] starting `node ./bin/www start` 
test:mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd 
test the dbURI:mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd 
Mongoose connected to mongodb://localhost/smr_pjt_loctog 
Mongoose log disconnected 
Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials. 
Mongoose connected to mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd 
Succeeded connected to: mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd 

但是,當我推到Heroku的,它失敗了,如下圖所示的錯誤消息:

2017-06-14T11:19:43.117450+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 

它說,連接到本地服務器時,我已經失敗了。不過,我已正確設置Heroku的配置var和可以看出以下形式:

[email protected]:~/Work/smr_pjt_loct_dr$ heroku config 
=== smr-pjt-loct-dr Config Vars 
MONGODB_URI: mongodb://heroku_60bjp5jd:[email protected]:15712/heroku_60bjp5jd 
NODE_ENV: production 

至於MLAB數據庫URI,我可以在db.js文件中使用下面的代碼獲得它:

if (process.env.NODE_ENV === 'production') { 
console.log("test:" + process.env.MONGODB_URI); 
dbURI = process.env.MONGODB_URI; 
console.log('test the dbURI:' + dbURI);} 

我想問題是關於貓鼬,但我嘗試了很多方法, 如升級並指定package.json文件中的節點和NPM的版本,或者安裝MongoDB驅動程序。

但它不能正常工作,我已經刪除了下面的貓鼬連接代碼:

mongoose.connect(dbURI, function (err, res) { 
    if (err) { 
    console.log ('ERROR connecting to: ' + dbURI + '. ' + err); 
    } else { 
    console.log ('Succeeded connected to: ' + dbURI); 
    } 
}); 

和錯誤消息仍然是相同的:

2017-06-14T11:19:43.117450+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 

實際上我通過設置MLAB數據庫heroku插件正確可以從image description here

有沒有人可以幫助我?確實謝謝!

更新:

至於錯誤信息,我發現形式Heroku的日誌:

2017-06-14T16:21:49.064801+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017] 
2017-06-14T16:21:49.064802+00:00 app[web.1]:  at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:328:35) 
2017-06-14T16:21:49.064803+00:00 app[web.1]:  at emitOne (events.js:96:13) 
2017-06-14T16:21:49.064804+00:00 app[web.1]:  at Pool.emit (events.js:191:7) 
2017-06-14T16:21:49.064805+00:00 app[web.1]:  at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12) 
2017-06-14T16:21:49.064805+00:00 app[web.1]:  at Object.onceWrapper (events.js:293:19) 
2017-06-14T16:21:49.064806+00:00 app[web.1]:  at emitTwo (events.js:106:13) 
2017-06-14T16:21:49.064807+00:00 app[web.1]:  at Connection.emit (events.js:194:7) 
2017-06-14T16:21:49.064808+00:00 app[web.1]:  at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49) 
2017-06-14T16:21:49.064808+00:00 app[web.1]:  at Object.onceWrapper (events.js:293:19) 
2017-06-14T16:21:49.064809+00:00 app[web.1]:  at emitOne (events.js:96:13) 
2017-06-14T16:21:49.064809+00:00 app[web.1]:  at Socket.emit (events.js:191:7) 
2017-06-14T16:21:49.064810+00:00 app[web.1]:  at emitErrorNT (net.js:1279:8) 
2017-06-14T16:21:49.064811+00:00 app[web.1]:  at _combinedTickCallback (internal/process/next_tick.js:80:11) 
2017-06-14T16:21:49.064812+00:00 app[web.1]:  at process._tickCallback (internal/process/next_tick.js:104:9) 

我還沒有發現任何這是關係到mongose,似乎MongoDB的核心應負責連接數據庫,但是,我根本沒有使用這個npm_module。

我試圖刪除整個node_module文件夾,並使用npm install來重新安裝它們,但仍然無法工作。

此外,我試圖讓我的本地主機的使用netstat:

[email protected]:~/Work/smr_pjt_loct_dr$ sudo netstat -atunlp | grep "27017" 
tcp  0  0 127.0.0.1:27017   0.0.0.0:*    LISTEN  1034/mongod 

解決:

問題解決了,感謝@vsenko的幫助下,哈哈哈! 我有另一個本地數據庫連接,我沒有注意到它。

回答

0

據我所看到的,您會收到不包含你寫慰問字符串錯誤消息是連接失敗的情況下: console.log ('ERROR connecting to: ' + dbURI + '. ' + err); 所以我最好的猜測是錯誤從另一個嘗試接收起源連接到相同或另一個數據庫。

+0

是的,mongoose.connect似乎沒用,它不會在控制檯中顯示任何消息。log(),但實際上,當我使用localhost訪問Mlab數據庫時,它實際上工作,它實際上讓我困惑。而且我在我的代碼中找不到任何可用於連接數據庫的地方。 –

+0

所有的數據庫連接代碼都在db.js文件中。 –

+0

但你輸出的這一行怎麼樣:'Mongoose連接到mongodb:// localhost/smr_pjt_loctog'? – vsenko

相關問題