2

My app works great when I hardcode the Mongo database connection, but when I try to use an environment variable, I get the errors in the log dump below.

I set the environment variable as follows:

$ heroku config:set MONGODB_URI='mongodb+srv://user:password@cluster0-ua7mc.mongodb.net/local_library?retryWrites=true'

(Note: I tried multiple iterations with and without quotes, and made sure the username and password were correct, etc.)

Here is the log dump:


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 - -

Code dealing with Mongo Connection and environment variable is here:

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;
}

Many thanks in advance if any ideas!

SOLUTION: Changing Whitelist IP to 0.0.0.0/0 fixed the problem.

Crowdpleasr
  • 3,574
  • 4
  • 21
  • 37

1 Answers1

2

Try to set the variable via the dashboard. If still not connect then the problem should be with your mongo URI.

enter image description here

Dilshan
  • 2,797
  • 1
  • 8
  • 26
  • Thank you for the suggestion. I did the following: 1. I set the new and old variable in the dashboard as you suggested. 2. I hard-coded the new and old `config` setting into my code. Doing so ,I was able to isolate that the problem definitely is in the new `config` setting `mongodb+srv://user:password@cluster0-ua7mc.mongodb.net/local_library?retryWrites=true`. But I've reviewed the setting MULTIPLE times and I can't find anything wrong with it. and are same for the new and old `config`. The ONLY difference is the `cluster` `r92qb` vs `ua7mc`. Everything else is identical. – Crowdpleasr Apr 27 '19 at 06:17
  • 1
    Check this link. It will help. https://stackoverflow.com/questions/52153538/what-is-a-transienttransactionerror-in-mongoose-or-mongodb Search these keyword 'MongoNetworkError TransientTransactionError'. I'm sorry I'm not expert to tell what exactly is the problem. Hope this will help :) – Dilshan Apr 27 '19 at 10:59
  • 1
    Thank you! Changing Whitelist IP to 0.0.0.0/0 fixed the problem! – Crowdpleasr Apr 27 '19 at 16:20