我与mlab的数据库连接存在问题,尽管对于本地数据库,代码可以正常工作。
我出于教育目的制作了一个简单的待办事项清单,并希望将其部署到Heroku。按照教程中的说明,我创建了heroku存储库,添加了mlab插件,并在其中创建了用户名和密码。
我想它应该自动定义MONGODB_URI或否?
在节点server.js中,我写道:
const url = process.env.MONGODB_URI || 'mongodb://localhost:27017/todos';
heroku日志说
2018-08-09T11:51:29.611169+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-08-09T11:51:27.000000+00:00 app[api]: Build succeeded
2018-08-09T11:51:33.285304+00:00 heroku[web.1]: State changed from starting to up
2018-08-09T11:51:49.935877+00:00 heroku[router]: at=info method=GET path="/" host=mysterious-mesa-82462.herokuapp.com request_id=6a87fa62-f64e-4286-8fd4-9df74897fed3 fwd="46.228.13.198" dyno=web.1 connect=1ms service=14ms status=200 bytes=430 protocol=https
2018-08-09T11:51:50.145583+00:00 heroku[router]: at=info method=GET path="/public/bundle.js" host=mysterious-mesa-82462.herokuapp.com request_id=c9ddd22d-b03c-454f-9fdb-3591e8d3d84a fwd="46.228.13.198" dyno=web.1 connect=1ms service=47ms status=200 bytes=1219774 protocol=https
2018-08-09T11:51:53.663137+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mysterious-mesa-82462.herokuapp.com request_id=9f7dc5b6-2353-4c93-b165-8aab22e6c041 fwd="46.228.13.198" dyno=web.1 connect=2ms service=2ms status=200 bytes=430 protocol=https
2018-08-09T11:51:54.273723+00:00 heroku[web.1]: Process exited with status 1
2018-08-09T11:51:54.213858+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=fbf4eaab-250c-4a58-ba16-7904c714209f fwd="46.228.13.198" dyno=web.1 connect=0ms service=80ms status=503 bytes=0 protocol=https
2018-08-09T11:51:54.149257+00:00 app[web.1]: (node:4) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2018-08-09T11:51:54.206924+00:00 app[web.1]: /app/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
2018-08-09T11:51:54.206930+00:00 app[web.1]: throw err;
2018-08-09T11:51:54.206935+00:00 app[web.1]: ^
2018-08-09T11:51:54.206937+00:00 app[web.1]:
2018-08-09T11:51:54.206941+00:00 app[web.1]: TypeError: Cannot read property 'db' of null
2018-08-09T11:51:54.206942+00:00 app[web.1]: at /app/server/index.js:25:12
2018-08-09T11:51:54.206946+00:00 app[web.1]: at err (/app/node_modules/mongodb/lib/utils.js:415:14)
2018-08-09T11:51:54.206947+00:00 app[web.1]: at executeCallback (/app/node_modules/mongodb/lib/utils.js:404:25)
2018-08-09T11:51:54.206951+00:00 app[web.1]: at err (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:21)
2018-08-09T11:51:54.206953+00:00 app[web.1]: at connectCallback (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
2018-08-09T11:51:54.206954+00:00 app[web.1]: at process.nextTick (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
2018-08-09T11:51:54.206955+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:131:7)
2018-08-09T11:51:54.206956+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:180:9)
2018-08-09T11:51:54.289103+00:00 heroku[web.1]: State changed from up to crashed
2018-08-09T11:51:54.291366+00:00 heroku[web.1]: State changed from crashed to starting
2018-08-09T11:51:58.298239+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-08-09T11:52:00.386282+00:00 heroku[web.1]: State changed from starting to up
2018-08-09T11:52:01.012824+00:00 heroku[web.1]: Process exited with status 1
2018-08-09T11:52:00.966718+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=61c973dc-b90e-47bf-91d1-a4882cb8d4f4 fwd="46.228.13.198" dyno=web.1 connect=0ms service=96ms status=503 bytes=0 protocol=https
2018-08-09T11:52:00.889939+00:00 app[web.1]: (node:4) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2018-08-09T11:52:00.949044+00:00 app[web.1]: /app/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
2018-08-09T11:52:00.949047+00:00 app[web.1]: throw err;
2018-08-09T11:52:00.949048+00:00 app[web.1]: ^
2018-08-09T11:52:00.949049+00:00 app[web.1]:
2018-08-09T11:52:00.949051+00:00 app[web.1]: TypeError: Cannot read property 'db' of null
2018-08-09T11:52:00.949052+00:00 app[web.1]: at /app/server/index.js:49:31
2018-08-09T11:52:00.949053+00:00 app[web.1]: at err (/app/node_modules/mongodb/lib/utils.js:415:14)
2018-08-09T11:52:00.949055+00:00 app[web.1]: at executeCallback (/app/node_modules/mongodb/lib/utils.js:404:25)
2018-08-09T11:52:00.949056+00:00 app[web.1]: at err (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:21)
2018-08-09T11:52:00.949057+00:00 app[web.1]: at connectCallback (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
2018-08-09T11:52:00.949059+00:00 app[web.1]: at process.nextTick (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
2018-08-09T11:52:00.949060+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:131:7)
2018-08-09T11:52:00.949061+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:180:9)
2018-08-09T11:52:01.050493+00:00 heroku[web.1]: State changed from up to crashed
2018-08-09T11:52:00.967244+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=4ad0d1e2-f161-47a8-828f-ce914f2bcd87 fwd="46.228.13.198" dyno=web.1 connect=0ms service=116ms status=503 bytes=0 protocol=https
2018-08-09T11:52:06.233488+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=2ea75f68-3000-45ec-ba3f-bb25b73296a4 fwd="46.228.13.198" dyno=web.1 connect=5001ms service= status=503 bytes= protocol=https
2018-08-09T11:59:15.000000+00:00 app[api]: Build started by user kbogdanov1@gmail.com
2018-08-09T11:59:46.488215+00:00 heroku[web.1]: State changed from crashed to starting
2018-08-09T11:59:46.007933+00:00 app[api]: Deploy 9b843e6f by user kbogdanov1@gmail.com
2018-08-09T11:59:46.007933+00:00 app[api]: Release v5 created by user kbogdanov1@gmail.com
2018-08-09T11:59:49.000000+00:00 app[api]: Build succeeded
2018-08-09T11:59:52.131125+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-08-09T11:59:55.387971+00:00 heroku[web.1]: State changed from starting to up
2018-08-09T12:00:20.220943+00:00 heroku[router]: at=info method=GET path="/" host=mysterious-mesa-82462.herokuapp.com request_id=717e5a08-b69a-4503-9dc0-75f2173edacf fwd="46.228.13.198" dyno=web.1 connect=0ms service=22ms status=304 bytes=150 protocol=https
2018-08-09T12:00:20.456261+00:00 heroku[router]: at=info method=GET path="/public/bundle.js" host=mysterious-mesa-82462.herokuapp.com request_id=d2913dab-8240-4865-91b1-bb22849e472b fwd="46.228.13.198" dyno=web.1 connect=1ms service=66ms status=200 bytes=1219774 protocol=https
2018-08-09T12:00:25.919525+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mysterious-mesa-82462.herokuapp.com request_id=a1078408-3ab9-431f-a88b-ff1ccd52792f fwd="46.228.13.198" dyno=web.1 connect=0ms service=4ms status=304 bytes=150 protocol=https
2018-08-09T12:00:26.499126+00:00 heroku[router]: at=info method=GET path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=8418118a-11b0-4637-ba4f-54ca343302b0 fwd="46.228.13.198" dyno=web.1 connect=0ms service=194ms status=200 bytes=158 protocol=https
2018-08-09T12:00:26.394055+00:00 app[web.1]: (node:4) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2018-08-09T12:00:28.211184+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=d7dad439-d3c1-4b8a-8c4c-10e50bff2b36 fwd="46.228.13.198" dyno=web.1 connect=0ms service=125ms status=400 bytes=126 protocol=https
2018-08-09T12:00:30.001657+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=7c805b9f-0c10-404c-b67e-9dd35241ce7f fwd="46.228.13.198" dyno=web.1 connect=0ms service=44ms status=400 bytes=126 protocol=https
2018-08-09T12:00:31.993509+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=8243576b-4d85-433f-9136-fc49943dc992 fwd="46.228.13.198" dyno=web.1 connect=0ms service=91ms status=400 bytes=126 protocol=https
2018-08-09T12:03:57.064265+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=854ae74a-1c5b-4ce0-8f6b-18809220a0e4 fwd="46.228.13.198" dyno=web.1 connect=1ms service=49ms status=400 bytes=126 protocol=https
2018-08-09T12:05:31.585925+00:00 heroku[router]: at=info method=GET path="/" host=mysterious-mesa-82462.herokuapp.com request_id=8d3bb93e-38ac-47fa-bfdc-787f90660f57 fwd="46.228.13.198" dyno=web.1 connect=1ms service=2ms status=304 bytes=150 protocol=https
2018-08-09T12:05:31.763156+00:00 heroku[router]: at=info method=GET path="/public/bundle.js" host=mysterious-mesa-82462.herokuapp.com request_id=4f8dffa9-54bd-43bf-9511-dc280781ffe6 fwd="46.228.13.198" dyno=web.1 connect=1ms service=4ms status=304 bytes=240 protocol=https
2018-08-09T12:05:32.008909+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mysterious-mesa-82462.herokuapp.com request_id=b891e445-f406-41f5-84ed-3450424b3e9f fwd="46.228.13.198" dyno=web.1 connect=1ms service=3ms status=200 bytes=430 protocol=https
2018-08-09T12:05:32.462782+00:00 heroku[router]: at=info method=GET path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=67ffca08-654a-42d3-96e6-385ea43be780 fwd="46.228.13.198" dyno=web.1 connect=0ms service=50ms status=304 bytes=149 protocol=https
2018-08-09T12:05:33.292564+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=07a6bca7-181e-4a7b-ac55-b60052c25083 fwd="46.228.13.198" dyno=web.1 connect=0ms service=21ms status=400 bytes=126 protocol=https
2018-08-09T12:07:35.000000+00:00 app[api]: Build started by user kbogdanov1@gmail.com
2018-08-09T12:08:09.111189+00:00 app[api]: Deploy ee671fb9 by user kbogdanov1@gmail.com
2018-08-09T12:08:09.795444+00:00 heroku[web.1]: Restarting
2018-08-09T12:08:09.796029+00:00 heroku[web.1]: State changed from up to starting
2018-08-09T12:08:09.111189+00:00 app[api]: Release v6 created by user kbogdanov1@gmail.com
2018-08-09T12:08:10.959234+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-08-09T12:08:11.077248+00:00 heroku[web.1]: Process exited with status 143
2018-08-09T12:08:13.000000+00:00 app[api]: Build succeeded
2018-08-09T12:08:18.098711+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-08-09T12:08:23.604546+00:00 heroku[web.1]: State changed from starting to up
2018-08-09T12:08:36.970198+00:00 heroku[router]: at=info method=GET path="/" host=mysterious-mesa-82462.herokuapp.com request_id=ba4cbfdc-8c15-459e-b2c6-8afb592c3af0 fwd="46.228.13.198" dyno=web.1 connect=0ms service=32ms status=304 bytes=150 protocol=https
2018-08-09T12:08:37.196677+00:00 heroku[router]: at=info method=GET path="/public/bundle.js" host=mysterious-mesa-82462.herokuapp.com request_id=84e0db79-1ddd-4a3b-8c3b-d859c5033497 fwd="46.228.13.198" dyno=web.1 connect=0ms service=58ms status=200 bytes=1219774 protocol=https
2018-08-09T12:08:42.798364+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mysterious-mesa-82462.herokuapp.com request_id=91883535-9b20-4ff7-bb11-e3cd83461c66 fwd="46.228.13.198" dyno=web.1 connect=0ms service=4ms status=200 bytes=430 protocol=https
2018-08-09T12:08:43.520490+00:00 heroku[router]: at=info method=GET path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=1cd44f56-04f3-40bd-847d-26ff89437932 fwd="46.228.13.198" dyno=web.1 connect=1ms service=217ms status=304 bytes=149 protocol=https
2018-08-09T12:08:43.333362+00:00 app[web.1]: (node:4) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2018-08-09T12:08:48.455420+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=1eb49823-c4a2-4824-8d85-9940589d3d73 fwd="46.228.13.198" dyno=web.1 connect=1ms service=92ms status=400 bytes=126 protocol=https
2018-08-09T12:08:53.517480+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=943e6d3f-3a5b-46d2-a10f-818911e1247f fwd="46.228.13.198" dyno=web.1 connect=1ms service=26ms status=400 bytes=126 protocol=https
2018-08-09T12:08:57.532474+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=7753069f-90d7-493b-949e-4c1fc363a68a fwd="46.228.13.198" dyno=web.1 connect=1ms service=26ms status=400 bytes=126 protocol=https
2018-08-09T12:43:56.160060+00:00 heroku[web.1]: Idling
2018-08-09T12:43:56.160539+00:00 heroku[web.1]: State changed from up to down
2018-08-09T12:43:57.424579+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-08-09T12:43:57.560892+00:00 heroku[web.1]: Process exited with status 143
2018-08-09T13:24:36.000000+00:00 app[api]: Build started by user kbogdanov1@gmail.com
2018-08-09T13:25:05.162553+00:00 heroku[web.1]: State changed from down to starting
2018-08-09T13:25:04.999479+00:00 app[api]: Deploy 129ad720 by user kbogdanov1@gmail.com
2018-08-09T13:25:04.999479+00:00 app[api]: Release v7 created by user kbogdanov1@gmail.com
2018-08-09T13:25:08.000000+00:00 app[api]: Build succeeded
2018-08-09T13:25:10.503939+00:00 heroku[web.1]: Starting process with command `node server/index.js`
2018-08-09T13:25:13.694005+00:00 heroku[web.1]: State changed from starting to up
2018-08-09T13:26:40.200581+00:00 heroku[router]: at=info method=GET path="/" host=mysterious-mesa-82462.herokuapp.com request_id=38a6ed8d-2283-4d2a-9001-36ccedb2f9bf fwd="46.228.13.198" dyno=web.1 connect=0ms service=17ms status=304 bytes=150 protocol=https
2018-08-09T13:26:46.696756+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=mysterious-mesa-82462.herokuapp.com request_id=9a8f3fc8-1c75-4f91-b645-d6d04d05fd20 fwd="46.228.13.198" dyno=web.1 connect=0ms service=2ms status=304 bytes=150 protocol=https
2018-08-09T13:26:47.114574+00:00 app[web.1]: (node:4) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2018-08-09T13:26:47.209103+00:00 heroku[router]: at=info method=GET path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=38a65a0c-749d-4e0f-8618-858700a95e98 fwd="46.228.13.198" dyno=web.1 connect=1ms service=106ms status=304 bytes=149 protocol=https
2018-08-09T13:26:48.943901+00:00 heroku[router]: at=info method=POST path="/api/todos" host=mysterious-mesa-82462.herokuapp.com request_id=48e3c6ed-8ead-493d-b70c-2c08042a0a94 fwd="46.228.13.198" dyno=web.1 connect=1ms service=41ms status=400 bytes=126 protocol=https
好像看不到该数据库
尽管heroku配置显示此变量
MONGODB_URI: mongodb://heroku_tj29hkq4:MY_PASSWORD.mlab.com:17452/heroku_tj29hkq4
但是如果我像
那样手动粘贴
const url = 'mongodb://heroku_tj29hkq4:MY_PASSWORD.mlab.com:17452/heroku_tj29hkq4';
效果不好
实际上,如果我使用此数据库路径在本地运行它会导致相同的错误,所以我猜问题不在于Heroku,而在于mlab的db中。
可能我忘了添加一些东西吗?
答案 0 :(得分:0)
听起来您的MONGODB_URI
可能与附加组件不同步。要检查此情况,请尝试登录附加管理页面(Heroku仪表板> App>资源>向下滚动,直到看到mlab,然后单击链接-或者可以运行heroku addons:open mongolab -a your_app_name_here
)。从这里,您应该能够检查数据库的最新凭据。
如果这些值与MONGODB_URI
中的值不同,则意味着您可能已对该配置变量进行了手动更新。进行手动更新后,附加组件将不再能够自动更新-这是一项安全功能,可阻止附加组件清除您输入的数据。
要恢复自动更新,您可以尝试从应用程序中删除MONGODB_URI
配置变量,然后使用--as MONGODB_URI
选项重新附加附件。例如,
heroku addons:attach <addon-name-here> --as MONGODB_URI -a your_app_name_here
通过查看以下内容可以找到<addon-name-here>
部分
heroku addons -a your_app_name_here
其格式为word-anotherword-12345
。