如何使用mongoose连接到Heroku上的mongodb数据库?

时间:2014-04-17 22:40:23

标签: javascript node.js mongodb heroku mongoose

目前我的应用在本地工作正常,可以连接到我的本地数据库。

现在这是我在heroku上运行时遇到的错误:

2014-04-17T06:32:23.404458+00:00 app[web.1]: > application-name@0.0.1 start /app
2014-04-17T06:32:23.404460+00:00 app[web.1]: > node app.js
2014-04-17T06:32:24.052231+00:00 app[web.1]: /public/images/test_image.jpg
2014-04-17T06:32:24.090050+00:00 app[web.1]: Database connection error, fcukkk: [Error: failed to connect to [localhost:27017]]
2014-04-17T06:32:24.084553+00:00 app[web.1]: Express server listening on port 23569
2014-04-17T06:32:24.630834+00:00 heroku[web.1]: State changed from starting to up
2014-04-17T06:33:08.084028+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=pure-cliffs-4076.herokuapp.com request_id=9bf3db67-e00e-428a-896c-26c03dfa01ff fwd="68.99.126.105" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0
2014-04-17T06:33:08.094792+00:00 app[web.1]: GET / 200 29995ms
2014-04-17T06:33:08.624842+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=pure-cliffs-4076.herokuapp.com request_id=d09fe61e-f608-47fd-9b1f-a5b4ea54a2a9 fwd="68.99.126.105" dyno=web.1 connect=1ms service=8ms status=404 bytes=193
2014-04-17T06:33:08.620901+00:00 app[web.1]: GET /favicon.ico 404 5ms
2014-04-17T06:33:44.013253+00:00 heroku[web.1]: State changed from up to starting
2014-04-17T06:33:43.925737+00:00 heroku[api]: Add mongolab:sandbox add-on by marshall.a.x@gmail.com
2014-04-17T06:33:43.925801+00:00 heroku[api]: Release v4 created by marshall.a.x@gmail.com
2014-04-17T06:33:47.537662+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-04-17T06:33:48.704235+00:00 he
roku[web.1]: Starting process with command `npm start`
2014-04-17T06:33:49.868707+00:00 app[web.1]: > application-name@0.0.1 start /app
2014-04-17T06:33:49.868699+00:00 app[web.1]: 
2014-04-17T06:33:49.868711+00:00 app[web.1]: 
2014-04-17T06:33:49.868709+00:00 app[web.1]: > node app.js
2014-04-17T06:33:50.254949+00:00 app[web.1]: /public/images/test_image.jpg
2014-04-17T06:33:50.274924+00:00 app[web.1]: Database connection error, fcukkk: [Error: failed to connect to [localhost:27017]]
2014-04-17T06:33:50.271844+00:00 app[web.1]: Express server listening on port 21153
2014-04-17T06:33:49.631038+00:00 heroku[web.1]: Process exited with status 143
2014-04-17T06:33:50.361358+00:00 heroku[web.1]: State changed from starting to up
2014-04-17T07:38:52.772530+00:00 heroku[web.1]: Idling
2014-04-17T07:38:52.773249+00:00 heroku[web.1]: State changed from up to down
2014-04-17T07:38:56.398500+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-04-17T07:38:59.466635+00:00 heroku[web.1]: Process exited with status 143
2014-04-17T18:18:34.665450+00:00 heroku[web.1]: Unidling
2014-04-17T18:18:34.665450+00:00 heroku[web.1]: State changed from down to starting
2014-04-17T18:18:39.773345+00:00 heroku[web.1]: Starting process with command `npm start`
2014-04-17T18:18:41.209327+00:00 app[web.1]: 
2014-04-17T18:18:41.209334+00:00 app[web.1]: > application-name@0.0.1 start /app
2014-04-17T18:18:41.209336+00:00 app[web.1]: > node app.js
2014-04-17T18:18:41.209466+00:00 app[web.1]: 
2014-04-17T18:18:41.831107+00:00 app[web.1]: /public/images/test_image.jpg
2014-04-17T18:18:41.863975+00:00 app[web.1]: Database connection error, fcukkk: [Error: failed to connect to [localhost:27017]]
2014-04-17T18:18:41.859394+00:00 app[web.1]: Express server listening on port 56521
2014-04-17T18:18:41.983113+00:00 heroku[web.1]: State changed from starting to up
2014-04-17T18:19:12.750685+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=pure-cliffs-4076.herokuapp.com request_id=b7e7cab0-e79f-4457-9e5d-1acbfd494ef0 fwd="68.99.126.105" dyno=web.1 connect=5ms service=30001ms status=503 bytes=0
2014-04-17T18:19:12.754834+00:00 app[web.1]: GET / 200 29995ms
2014-04-17T19:21:21.596778+00:00 heroku[web.1]: Idling
2014-04-17T19:21:21.597514+00:00 heroku[web.1]: State changed from up to down
2014-04-17T19:21:26.046285+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-04-17T19:21:28.181093+00:00 heroku[web.1]: Process exited with status 143
2014-04-17T20:56:21.949618+00:00 heroku[web.1]: Unidling
2014-04-17T20:56:21.949987+00:00 heroku[web.1]: State changed from down to starting
2014-04-17T20:56:31.657944+00:00 heroku[web.1]: Starting process with command `npm start`
2014-04-17T20:56:33.655866+00:00 app[web.1]: 
2014-04-17T20:56:33.655871+00:00 app[web.1]: > application-name@0.0.1 start /app
2014-04-17T20:56:33.655873+00:00 app[web.1]: > node app.js
2014-04-17T20:56:33.655875+00:00 app[web.1]: 
2014-04-17T20:56:34.682567+00:00 app[web.1]: /public/images/test_image.jpg
2014-04-17T20:56:34.706650+00:00 app[web.1]: Database connection error, fcukkk: [Error: failed to connect to [localhost:27017]]
2014-04-17T20:56:34.703013+00:00 app[web.1]: Express server listening on port 21923
2014-04-17T20:56:35.308394+00:00 heroku[web.1]: State changed from starting to up
2014-04-17T20:57:08.004131+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=pure-cliffs-4076.herokuapp.com request_id=033a5a44-68a3-4b1c-8f6c-5755371d076d fwd="68.99.126.105" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0
2014-04-17T20:57:08.005107+00:00 app[web.1]: GET / 200 29992ms
2014-04-17T21:17:36.623084+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=pure-cliffs-4076.herokuapp.com request_id=1117b0b1-da9d-4fef-ad79-f1a5c4178807 fwd="68.99.126.105" dyno=web.1 connect=2ms service=30000ms status=503 bytes=0
2014-04-17T21:17:36.625513+00:00 app[web.1]: GET / 200 30000ms
2014-04-17T22:07:00.807923+00:00 heroku[web.1]: Idling
2014-04-17T22:07:00.809134+00:00 heroku[web.1]: State changed from up to down
2014-04-17T22:07:04.732639+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-04-17T22:07:07.514765+00:00 heroku[web.1]: Process exited with status 143
2014-04-17T22:32:27+00:00 heroku[slug-compiler]: Slug compilation started
2014-04-17T22:33:00+00:00 heroku[slug-compiler]: Slug compilation finished
2014-04-17T22:33:00.065665+00:00 heroku[web.1]: State changed from down to starting
2014-04-17T22:32:59.921981+00:00 heroku[api]: Deploy 0e3a24a by marshall.a.x@gmail.com
2014-04-17T22:32:59.922073+00:00 heroku[api]: Release v5 created by marshall.a.x@gmail.com
2014-04-17T22:33:05.771395+00:00 heroku[web.1]: Starting process with command `npm start`
2014-04-17T22:33:07.654364+00:00 app[web.1]: > application-name@0.0.1 start /app
2014-04-17T22:33:07.654367+00:00 app[web.1]: 
2014-04-17T22:33:07.654366+00:00 app[web.1]: > node app.js
2014-04-17T22:33:07.654359+00:00 app[web.1]: 
2014-04-17T22:33:08.435746+00:00 app[web.1]: /public/images/test_image.jpg
2014-04-17T22:33:08.474993+00:00 app[web.1]: Express server listening on port 20321
2014-04-17T22:33:08.771030+00:00 app[web.1]: sup stud, Database connected.
2014-04-17T22:33:08.770901+00:00 app[web.1]: Succeeded connected to: mongodb://mongodb://heroku_app24230415:l91npmc6qlrdole3ptlq0s8not@ds045157.mongolab.com:45157/heroku_app24230415/images
2014-04-17T22:33:08.577315+00:00 heroku[web.1]: State changed from starting to up
2014-04-17T22:33:48.606992+00:00 app[web.1]: []
2014-04-17T22:33:49.143625+00:00 heroku[router]: at=info method=GET path=/ host=pure-cliffs-4076.herokuapp.com request_id=982dd016-e68c-4190-a41e-c3a6780a06ef fwd="68.99.126.105" dyno=web.1 connect=2ms service=624ms status=500 bytes=7683
2014-04-17T22:33:49.176065+00:00 app[web.1]:     13|                   form(method="POST" action="/view/#{image._id}" enctype="multipart/form-data")
2014-04-17T22:33:49.176067+00:00 app[web.1]:     14|                     input(type="image" src="/images/#{image.source}" name="bigImage" width="300")
2014-04-17T22:33:49.176068+00:00 app[web.1]: 
2014-04-17T22:33:49.176060+00:00 app[web.1]:     10|               li
2014-04-17T22:33:49.176070+00:00 app[web.1]: Cannot read property 'source' of undefined
2014-04-17T22:33:49.176050+00:00 app[web.1]: TypeError: /app/views/index.jade:11
2014-04-17T22:33:49.176063+00:00 app[web.1]:     12|                 span
2014-04-17T22:33:49.176062+00:00 app[web.1]:   > 11|                 img(src="/images/#{image.source}" name="original")
2014-04-17T22:33:49.176058+00:00 app[web.1]:     9|             each image in images
2014-04-17T22:33:49.176078+00:00 app[web.1]:     at res (/app/node_modules/jade/lib/jade.js:173:38)
2014-04-17T22:33:49.176071+00:00 app[web.1]:     at eval (eval at <anonymous> (/app/node_modules/jade/lib/jade.js:172:8), <anonymous>:78:56)
2014-04-17T22:33:49.176073+00:00 app[web.1]:     at eval (eval at <anonymous> (/app/node_modules/jade/lib/jade.js:172:8), <anonymous>:165:4)
2014-04-17T22:33:49.176075+00:00 app[web.1]:     at eval (eval at <anonymous> (/app/node_modules/jade/lib/jade.js:172:8), <anonymous>:184:21)
2014-04-17T22:33:49.176079+00:00 app[web.1]:     at Object.exports.render (/app/node_modules/jade/lib/jade.js:269:10)
2014-04-17T22:33:49.176084+00:00 app[web.1]:     at View.render (/app/node_modules/express/lib/view.js:76:8)
2014-04-17T22:33:49.176081+00:00 app[web.1]:     at Object.exports.renderFile (/app/node_modules/jade/lib/jade.js:305:18)
2014-04-17T22:33:49.176082+00:00 app[web.1]:     at View.exports.renderFile [as engine] (/app/node_modules/jade/lib/jade.js:290:21)
2014-04-17T22:33:49.176087+00:00 app[web.1]:     at ServerResponse.res.render (/app/node_modules/express/lib/response.js:798:7)
2014-04-17T22:33:49.176085+00:00 app[web.1]:     at Function.app.render (/app/node_modules/express/lib/application.js:504:10)
2014-04-17T22:33:49.181859+00:00 app[web.1]: GET / 500 607ms

这是我的app.js文件的相关部分:

var uristring= 'mongodb://'+process.env.MONGOLAB_URI+'/images';

var theport = process.env.PORT  || 5000;
var appDir = path.dirname(require.main.filename);
mongoose.connect(uristring, function(err,res){
        if (err) {
                console.log('ERROR connecting to: '+uristring+'. ' + err)
        }
        else {
                console.log('Succeeded connected to: '+uristring);
        }
});
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'Database connection error, fcukkk:'));
conn.once('open', function callback() {console.log("sup stud, Database connected.")});

我很困惑,因为在文档https://devcenter.heroku.com/articles/nodejs-mongoose中没有提到实际创建我想要连接的数据库。我假设当你最初连接到db时,这是mongoose所做的事情。

1 个答案:

答案 0 :(得分:1)

您是否意识到您在日志结束时成功连接?它在localhost上寻找你的数据库的前3次:27017,但它第四次寻找正确的地址(似乎)。