Skip to content
Advertisement

Can’t connect to a MongoDB server

I’m trying to deploy this Fullstack app to a DigitalOcean droplet: https://github.com/maismin/stock-portfolio-app-demo

I installed MongoDB like how to documentation said and then I started it and didn’t do anything else (I don’t know if I need to actually make a database or not this is the first time I’m using MongoDB). Then I cloned the repo, then I installed NPM with npm install, then I just copied this in to the terminal:

PORT=3000
MONGODB_URI='LINK TO YOUR MONGDB SERVER'
MONGODB_URI_DEV='LINK TO YOUR LOCAL MONGODB SERVER'
MONGODB_URI_TEST='LINK TO LOCAL MONGODB SERVER'
IEX_URI=https://cloud.iexapis.com/stable
IEX_KEY='YOUR IEX KEY'
JWT_SECRET='YOUR SECRET'

and replace this MONGODB_URI='LINK TO YOUR MONGDB SERVER' with MONGODB_URI='xxx.xxx.xxxx' (my servers IP, this is where I really have no idea what to do and am just trying things). I also did the MONGODB_URI_DEV and MONGODB_URI_TEST with the same. The IEX and JWT variables are fine and I knew what to do there.

I got this error when I npm run dev:

See https://webpack.js.org/plugins/environment-plugin for example.
Error connection to MongoDB: The 'uri' parameter to 'openUri()' must be a string, got "undefined". Make sure the first parameter to 'mongoose.connect()' or 'mongoose.createConnection()' is a string.
[nodemon] app crashed - waiting for file changes before starting...

Advertisement

Answer

This error is due to invalid mongodb URL, ensure that it is correct and the .env or the file where the URL is stored is imported correctly..

it might also be the issue with the .env, so add the enviroment variables directly through digital Ocean: https://thecloudhub.com/tag/digitalocean-droplet-environment-variables/

4 People found this is helpful
Advertisement