Can't connect to a MongoDB server - node.js

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

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/

Did you add the .env file?
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'

Related

I can't access my .env variables!! nodeJS

Server is running on PORT: undefined in undefined mode.
MongoDB database connected with HOST: localhost
used to get Server is running on PORT: 4000 in development mode. and suddenly it's undefined for both and every variable in my .env, like cloudinary .. stripe ...
You can add this snippet before starting mongoDB to see what's getting to your node process env variables:
console.log(process.env)
Also note that you can pass inline environment variables to node process like this:
PORT=4000 node ./my-script.js
To automatically load .env files, you might be using this package: https://github.com/motdotla/dotenv so be sure that you have all packages installed (npm install or yarn install, depending on what you are using).
One of the first things you should pay attention to is the file path. Check where your .env file is located in the project. If it is in the root of the project, there is no need to do anything. Otherwise, you must specify the path to the .env file.

throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string'

I try to connect my database to express, but I've this message.. on PostgreSQL
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string'
if somebody had fix the same problem ;)
when you installed Postgresql did you set the password for Postgres user? Because I had this same issue and I solved it by doing this silly but apparently important step when you installed Postgres.
run the shell and then use the command \password.
Then should put this same password in the code you use to connect the database with express.
I had this same error message come up when I tried to connect my website running locally to my database running locally.
I was able to connect my local website to my live database. And my live website worked properly with my live database too.
But I couldn't connect my local website (or Postman) to my local database.
However, I just received help from a friend to fix this. I added this line to the top of my pool.js file:
import dotenv from 'dotenv/config';
The problem was that it wasn't accessing the environment variables from the pool.js file. Although, my friend said a better solution would be to add the 'dotenv/config' to a script in the package.json file, but he wasn't sure what the proper syntax was for that, so I'm going to try to find that out next.
It could be password value undefined/null. If you put the password inside .env file, make sure to config it. In my program when the password value was undefined, it logged this error.
The syntax is
nodemon -r dotenv/config src/server.js

How do I define Mongo connection in NodeJS app when deploying it on heroku

I am trying to deploy my first app to heroku using mLab MongoDB addon. How should I define the connection?
This is what I found in a NodeJS book, it does not seem to work, though. I replaced the password and database name with appropriate values.
mongoose.connect('mongodb://databasename:password#kahana.mongohq.com:10089/app26');
mongoose.connection.on('open', function() {
console.log('Mongoose connected.');
});
You should use Heroku enviroment variables. When you add a Mongo addon you should automatically have the enviroment variable available (these can also be accessed through the Heroku dashboard). So with MLAB you can get your connection string by accessing process.env.MONGOLAB_URI. So you can just do mongoose.connect(process.env.MONGOLAB_URI);
To get this to work on your localost you run heroku config > .env to export the enviroment variables to a .env file at the root of your app. Then when you run heroku local the MONGOLAB_URI env variable will be also available on your localhost.

Failed Database Connection between OpenShift Server and Mlab.com?

I'm hoping to host a node.js server at OpenShift, utilizing a MongoDB database hosted at mlab.com (the new version of mongolab.com) Here's a pretty straight forward tutorial. According to that tutorial, things seem pretty straight forward. That tutorial may be a bit dated, but seems to have been targeted directly for my application (less the update from Mongolab --> mlab) I've used Mongolab in the past and they provide a great service.
So I've built my database. I've written my node code and tested it from local host, where it works great. Yes there are a few lines of difference, but not much. I'm using the same git directory as I'm pushing to OpenShift. The code is pretty straight forward.
databaseUrl = 'mongodb://UserNameHere:PasswordHere#ds012345.mlab.com:12345/DataBaseName';
if (process.env.MLAB_URI) {
databaseUrl = process.env.MLAB_URI;
}
MongoClient.connect(databaseUrl, function(err, db) {
assert.equal(null, err, "Database Connection Troubles: " + err);
test process.env.MLAB_URI from my terminal after a RHC login.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $MLAB_URI
mongodb://<username>:<password>#ds012345.mlab.com:12345/DataBaseName
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> echo $OPENSHIFT_REPO_DIR
/var/lib/openshift/xxxxxxxxxxxx/app-root/runtime/repo/
Test with $, use in code with process.env. call. Obviously I've changed my username, password and Openshift server identification, but I've checked and there appear to be no spelling errors. I get the same fail on openshift if I don't use the MLAB_URI environment variable. It's like the connection from the OpenShift server is shut off.
Mlab does provide some tools to verify the connection to a MongoDB there. Here's link to the Mlab assist stuff. I can ping the mlab location from a RHC login and it works just fine. Unfortunately I'm unable to do the % netcat -w 3 -v ds012345.mlab.com 12345 test. That tool (netcat / nc) isn't available at OpenShift.
Again, this thing works fine when I run my node file.js from my local host. I can see data being deposited at the mlab server. It fails if I run from Openshift, with a
throw err ^
AssertionError: Database Connection Troubles: MongoError: auth failed
The code works fine if I use a MongoDB cartridge in the same gear at OpenShift. Unfortunately I've got a few different servers at different locations that are all sharing information. Anybody know what's going on here?
Update: I've done some additional testing from a terminal with RHC login to OpenShift.
[ABC-XYZ.rhcloud.com xxxxxxxxxxx]\> mongo ds012345.mlab.com:12345/dbName -u <dbuser> -p <password>;
MongoDB shell version: 2.4.9
connecting to: 127.6.xyz.xyz:27017/admin
Fri Mar 11 04:14:52.770 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
exception: login failed
The one surprise is that connecting to: url:27017/admin line... I'd like to understand that better. Stay tuned.
Update for anybody else who may get here. I submitted a support request to mlab. I received an immediate response (Awesome support!)
You'll need to upgrade your mongo shell version to 3.0+ in order to
connect and authenticate to an mLab Sandbox database server. It looks
like version 2.4.9 is being used.
So I was definitely using mongo shell version 3.0 from my localhost. I have little control # OpenShift for that command line feature.. But whoa... Let's not forget the big picture here. I'm really trying to use my node server to contact mlab via a var MongoClient = require('mongodb').MongoClient; connect call. Let's make the same check. Do I have the latest version of mongodb listed in my package.json file? Oops..
Easy fix. Update package.json to require a newer version of mongodb. Success at OpenShift. Yipee!

How to seed data for a node+mongo app deployed to Heroku?

I have a node express app with MongoDB as database. I want to have a seed.js file which I can run to fill in initial data to the database. I have no problem doing this on my local maschine. I just write the mongo commands in my seed.js file and run it with:
$ mongo localhost:27017/myApp seed.js
However when I deploy my app to Heroku including MongoLab I am not sure how to seed the data there. Google leads me to rails stuff most of the time.
So is there a simple way of seeding data for Heroku MongoLab without writing a script?
Update:
If I try to run it with the MONGOLAB_URI from heroku I get an error.
$ mongo mongodb://heroku_xxxx:xxxxx.mongolab.com:xxx/heroku_xxxx seed.js
MongoDB shell version: 3.0.5
connecting to: mongodb://heroku_xxxxxxxx
2015-08-19T21:44:22.694+0100 E QUERY Error: More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'; heroku_xxxxxxxxxxxxx
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Here the complete answer how to run a seed.js file with mongo commands for an app deployed to Heroku, thanks to pneumee and hunterloftis:
Get your MONGOLAB_URI by running $ heroku config inside your app directory
The MONGOLAB_URI variable has the format mongodb://user:pass#host:port/db.
Using those different parts stick together the correct terminal command to run the seed.js file:
$ mongo host:port/db -u user -p pass yourSeedFile.js
If you run heroku config -a yourapp you'll see the connection string for your mongolab instance. You can then connect to that database via the same mongo client you use locally, running the same seed.js.

Resources