0

I'm working on a web application with node.js and using Mongodb for database. everything is cool when I run my app in localhost. but when I run it in a shared host it gives an error which looks like its Mongoose connection problem.

here is my code:

const uri = 'mongodb://****:****@iranroboticacademy-bdziw.mongodb.net/test';

mongoose.connect(uri,{ useNewUrlParser: true });
mongoose.connection.once('open', function(){
  console.log('DataBase is connected.   ');

}).on('error', function(error){
  console.log('Connection error:', error);
});

and here is the erro that I get when I run the app in host:

Connection error: { MongoNetworkError: failed to connect to server [iranroboticacademy-shard-00-00-bdziw.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 3.226.212.92:27017]
    at Pool.<anonymous> (/home/iranrob1/public_html/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:182:13)
    at createConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:577:14)
    at connect (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:1021:9)
    at makeConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:31:7)
    at callback (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:247:5)
    at TLSSocket.err (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:276:7)
    at Object.onceWrapper (events.js:273:13)
    at TLSSocket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:6409) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [iranroboticacademy-shard-00-00-bdziw.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 3.226.212.92:27017]
    at Pool.<anonymous> (/home/iranrob1/public_html/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:182:13)
    at createConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:577:14)
    at connect (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/pool.js:1021:9)
    at makeConnection (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:31:7)
    at callback (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:247:5)
    at TLSSocket.err (/home/iranrob1/public_html/node_modules/mongodb/lib/core/connection/connect.js:276:7)
    at Object.onceWrapper (events.js:273:13)
    at TLSSocket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:6409) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6409) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
fdmx
  • 1
  • Does your shared host provide mongodb? Most of shared hosting plans does not provide database service for you to use. If no db is available - you can't connect to it. 1) If you use a VPS, you should [install and run mongodb](https://docs.mongodb.com/manual/administration/install-on-linux/) 2) If you are limited to use shared hosting you may want to check some db-as-a-service provider like https://mlab.com/ and host your db there. – Ivaylo Atanasov Jan 03 '20 at 20:42
  • I don't think that it support mongodb. maybe thats the problem. but what can I do then?! – fdmx Jan 03 '20 at 20:45
  • You can try with option 2). After registration you should be able to create a db via some UI and you will receive a connection uri with credentials. Use those as a value for your `const uri` variable in code. Good luck ;) – Ivaylo Atanasov Jan 03 '20 at 20:48
  • @fdmx : you need to check whether mongoDB hosted server is accepting connections from your node.js server, basically whitelist ip address of your node.js server in mongoDB server. – whoami - fakeFaceTrueSoul Jan 03 '20 at 21:09
  • Does this answer your question? [ECONNREFUSED error when connecting to mongodb from node.js](https://stackoverflow.com/questions/20386464/econnrefused-error-when-connecting-to-mongodb-from-node-js) – whoami - fakeFaceTrueSoul Jan 03 '20 at 21:13
  • no I'm not using mongodb locally!! – fdmx Jan 04 '20 at 06:12

3 Answers3

0

I have asked my host provider in a ticket and they said it doesn’t support mongodb. I have written everything with mongodb database in my application and I really can’t change it into other things. I need to use it in this host. Is there anyone who has a solution for this?!

Mattia Rasulo
  • 1,236
  • 10
  • 15
fdmx
  • 1
0

Sure you can use the free tier of MongoDB Cloud.

It’s actually much easier to mantain than the hosted version as you have visually all under control, automatic backups etc.

https://mongodb.com

Mattia Rasulo
  • 1,236
  • 10
  • 15
0

try to use 'connect to your aplication', copy the then change the

Stunggal
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '22 at 08:41