Heroku Node.js(express.js)应用程序本地工作但在使用MongoDB

时间:2017-06-14 12:13:10

标签: javascript node.js mongodb heroku

我有一个基于express.js的node.js应用程序,它在localhost上工作正常,它也可以从localhost连接Mlab数据库:

sheng@Takeshi:~/Work/smr_pjt_loct_dr$ NODE_ENV=production MONGODB_URI=mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com: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:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
test the dbURI:mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com: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:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
Succeeded connected to: mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com: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,如下所示:

sheng@Takeshi:~/Work/smr_pjt_loct_dr$ heroku config
=== smr-pjt-loct-dr Config Vars
MONGODB_URI: mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com: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文件中升级和指定Node和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]

我实际上正确地使用heroku插件设置了Mlab数据库,如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-core应该负责连接数据库,但是,我根本没有使用这个npm_module。

我试图删除整个node_module文件夹并使用npm install再次安装它们,但仍然无法正常工作。

另外,我试图获取localhost的netstat:

sheng@Takeshi:~/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的帮助,哈哈哈! 我有本地数据库的另一个连接,我没有注意到它。

1 个答案:

答案 0 :(得分:0)

据我所知,您收到的错误消息不包含您写入控制台的字符串是连接失败的情况: {{1}} 所以我最好的猜测是,您收到的错误源于另一次连接到相同或另一个数据库的尝试。