meanjs app,数据库连接失败(mongoHQ),字符串硬编码:
db: 'mongodb://user:pass@server.mongohq.com/db_name'
1) package.json: https://gist.github.com/anonymous/c864fbc2572bd10f7e90
2) log error: https://gist.github.com/anonymous/09726b7458560fe8e5f3
3) jitsu v0.14.0, node v0.10.33 and npm v2.1.8
4) platform : linux
上面的db string用于开发和生产环境以进行测试。问题是我可以在本地环境(dev env)中连接并向数据库添加条目,但是一旦我部署或推送到herokuapp或nodejitsu,我就会收到以下错误:
out Wed, 19 Nov 2014 23:39:34 GMT
out Wed, 19 Nov 2014 23:39:34 GMT Application loaded using the "production" environment configuration
out Wed, 19 Nov 2014 23:39:34 GMT
err Wed, 19 Nov 2014 23:39:36 GMT
err Wed, 19 Nov 2014 23:39:36 GMT events.js:72
err Wed, 19 Nov 2014 23:39:36 GMT ^
err Wed, 19 Nov 2014 23:39:36 GMT throw er; // Unhandled 'error' event
err Wed, 19 Nov 2014 23:39:36 GMT Error: failed to connect to [localhost:27017]
at null. (/opt/run/snapshot/package/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:549:74)
at emit (events.js:106:17)
at null. (/opt/run/snapshot/package/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
at emit (events.js:98:17)
at Socket. (/opt/run/snapshot/package/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
at Socket.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:419:13)
2014-11-19T23:27:22.425635+00:00 app[web.1]: MEAN.JS application started on port 15050
2014-11-19T23:27:22.430865+00:00 app[web.1]: events.js:72
2014-11-19T23:27:22.434306+00:00 app[web.1]: ^
2014-11-19T23:27:22.430483+00:00 app[web.1]:
2014-11-19T23:27:22.436997+00:00 app[web.1]: at emit (events.js:106:17)
2014-11-19T23:27:22.437004+00:00 app[web.1]: at Socket.emit (events.js:95:17)
2014-11-19T23:27:22.437006+00:00 app[web.1]: at net.js:440:14
2014-11-19T23:27:22.431432+00:00 app[web.1]: throw er; // Unhandled 'error' event
2014-11-19T23:27:22.436991+00:00 app[web.1]: Error: failed to connect to [localhost:27017]
2014-11-19T23:27:22.436996+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:549:74)
2014-11-19T23:27:22.436999+00:00 app[web.1]: at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
2014-11-19T23:27:22.437001+00:00 app[web.1]: at emit (events.js:98:17)
2014-11-19T23:27:22.437003+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
2014-11-19T23:27:22.437008+00:00 app[web.1]: at process._tickCallback (node.js:419:13)
2014-11-19T23:27:22.452476+00:00 app[web.1]: error: Forever detected script exited with code: 8
答案 0 :(得分:0)
我在自动增量插件所需的一个模型中找到了连接代码。
var connection = mongoose.createConnection('mongodb://localhost/n-express-dev');
替换为以下内容以使其无错误地部署。
var connection = mongoose.createConnection(process.env.MONGOHQ_URI || process.env.MONGOLAB_URI || 'mongodb://localhost/n-express-dev');