当我对Mongo数据库连接进行硬编码时,我的应用程序运行良好,但是当我尝试使用环境变量时,在下面的日志转储中得到了错误。
我将环境变量设置如下:
$ heroku config:set MONGODB_URI='mongodb+srv://user:password@cluster0-ua7mc.mongodb.net/local_library?retryWrites=true'
(注意:我尝试使用带引号和不带引号的多次迭代,并确保用户名和密码正确无误,等等。)
这是日志转储:
2019-04-26T23:54:22.761573+00:00 heroku[web.1]: Restarting
2019-04-26T23:54:22.768606+00:00 heroku[web.1]: State changed from up to starting
2019-04-26T23:54:22.587447+00:00 app[api]: Release v14 created by user user@hotmail.com
2019-04-26T23:54:22.587447+00:00 app[api]: Set MONGODB_URI config vars by user user@hotmail.com
2019-04-26T23:54:23.508862+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-04-26T23:54:23.588782+00:00 heroku[web.1]: Process exited with status 143
2019-04-26T23:54:26.341850+00:00 heroku[web.1]: Starting process with command `npm start`
2019-04-26T23:54:28.837322+00:00 app[web.1]:
2019-04-26T23:54:28.837353+00:00 app[web.1]: > express-locallibrary-tutorial@0.0.0 start /app
2019-04-26T23:54:28.837356+00:00 app[web.1]: > node ./bin/www
2019-04-26T23:54:28.837357+00:00 app[web.1]:
2019-04-26T23:54:30.638883+00:00 heroku[web.1]: State changed from starting to up
2019-04-26T23:54:30.921094+00:00 app[web.1]: MongoDB Connection Error { MongoNetworkError: connection 5 to cluster0-shard-00-02-ua7mc.mongodb.net:27017 closed
2019-04-26T23:54:30.921142+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:352:9)
2019-04-26T23:54:30.921145+00:00 app[web.1]: at Object.onceWrapper (events.js:273:13)
2019-04-26T23:54:30.921146+00:00 app[web.1]: at TLSSocket.emit (events.js:182:13)
2019-04-26T23:54:30.921148+00:00 app[web.1]: at _handle.close (net.js:610:12)
2019-04-26T23:54:30.921150+00:00 app[web.1]: at TCP.done (_tls_wrap.js:386:7)
2019-04-26T23:54:30.921151+00:00 app[web.1]: name: 'MongoNetworkError',
2019-04-26T23:54:30.921153+00:00 app[web.1]: errorLabels: [ 'TransientTransactionError' ],
2019-04-26T23:54:30.921155+00:00 app[web.1]: [Symbol(mongoErrorContextSymbol)]: {} }
2019-04-26T23:55:26.350787+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/catalog" host=immense-bucket-10578.herokuapp.com request_id=65fc54a9-5195-463b-a31a-f0ece0248800 fwd="47.136.227.118" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
2019-04-26T23:55:26.352964+00:00 app[web.1]: GET /catalog - - ms - -
有关Mongo Connection和环境变量的代码在这里:
var mongoose = require('mongoose');
var dev_db_url = 'mongodb+srv://user:password@cluster0-r92qb.mongodb.net/local_library?retryWrites=true';
var mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, {useNewUrlParser: true});
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
如果有什么想法,请多谢!
解决方案:将白名单IP更改为0.0.0.0/0可以解决此问题。