Unable to properly define MongoDB connection string as an Travis CI environmental variable - node.js

I'm working on my own project using Node.js and MongoDB as a database (using Mongoose).
When I developed it locally, I defined my environmental variables in a .env file and used 'env-cmd'.
Now I'm trying to create a testing workflow using Travis-CI without using a .env file so I defined my environmental variables on the repository setting but I keep getting an error that the connection string is invalid when trying to build:
It is worth mentioning that:
I escaped every special character when defining the env variable of the connection string.
I tried passing both process.env.MONGODB_URL with and without the ES6 backticks format to the mongoose.connect() method.
The connection string is valid (I tried running my test suites locally with the .env file and it worked)
The connection string is to the MongoDB Atlas database, not a local one.
I'm running the tests from as a docker container.
I can't figure out what is the problem, and if any additional information is needed to tackle this problem I will happily provide.
Thanks in advance!

Related

How To Fix: Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file Adonis Js

I got an exception from Redis it says:
Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file.
To Implement Throttle. I download Two Packages:
1) Adonis-Request-Throttler
yarn add adonis-request-throttler
2) Redis
yarn add #adonisjs/redis
And ConfigureThen as the instruction shown in Adonis Js Official Documentation.
node ace configure #adonisjs/packagename
I also pest Validate environment variables in env.ts file. But When I try to run my app I got an exception :
Please Help me To Fix This. I have tried everything but it's not working and my project is is running.
That problem is because your config/database file is not properly configured.
Set the variables as recommended in the documentation depending on which database driver you are using.

Can anyone tell me why is node not recognizing my environment variable?

I have a sendgrid API key. I placed it into a dot.env file inside a config folder.
Bud when I try to use it to set api key:
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
Node keeps telling me API key does not start with "SG."
If I try to console.log(process.env.SENDGRID_API_KEY) it gives me undefined.
inside dev.env file:
SENDGRID_API_KEY=myapikey
I also have a PORT variable inside this folder and it is used without problems, so it is not a matter of a path to the file. It is not a typo either.
Are you using something like the "dotenv" package to read de environment variables from the "dev.env" file? You can't read environment variables in NodeJS without something like that if I am not mistaken. When I learned how to import this kind of variables I was teached to name the file just ".env".

Node-Red mongodb3 connect DB using URL from environment variable

I'm running Node-Red embedded in Express application. Also using 'dotenv' to load environment variables.
For storage using MongoDB with 'node-red-contrib-mongodb3'.
Everything works as expected. But, I have different environments and different MongoDB for each environments.
I want to connect to MongoDB from configuration (.env file or environment file).
Something like, in MongoDB config node URL input box golbal.get('env').MONGODB_DEV_URL or msg.MONGODB_URL
Tried looking for an option in the documentation of 'mongodb3' and google, still no luck. Any help or direction will be appreciated.
From the Node-RED docs
Any node property can be set with an environment variable by setting
its value to a string of the form ${ENV_VAR}. When the runtime loads
the flows, it will substitute the value of that environment variable
before passing it to the node.
This only works if it replaces the entire property - it cannot be used
to substitute just part of the value. For example, it is not possible
to use CLIENT-${HOST}.

Set postgres database url on travis-ci for testing node.js application

I have written some tests for my node.js application and the tests are running locally using a Postgresql test database.
When I run my test script, npm run test, the environment is set to test and when this happens, the database connection string is set for the test database and my queries in the application are now done on the test database. Like so:
let connectionString;
if (process.env.NODE_ENV === 'test') {
connectionString =`postgresql://${process.env.DB_USER}:${process.env.DB_PASS}#${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.TEST_DB_NAME}`;
} else {
connectionString =`postgresql://${process.env.DB_USER}:${process.env.DB_PASS}#${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`;
}
This way my tests are being run on the test database.
On travis however, I know I am going to need to further configure its own database. On the travisCI docs, I read about how I could set up a PostgreSQL database here, but this doesn't help me because how do I get the full database URL as above? On travisCI, what am I to use as my database hostname or port and how do I set this value inside my code?
How do I set the database connection string and access it in my code?
Thanks for any ideas.
Found this while searching of how to do the exact same thing, and this didn't answer my question, so in case anyone else comes across it, I think I have solved it.
zerosand1s kind of answered it, but there seems to be nowhere online which actually says what the hostname or port should be (maybe I'm being dumb and this is obvious but :shrug:)
As per the psql docs:
Defaults to the value of the PGPORT environment variable or, if not set, to the port specified at compile time, usually 5432.
So I guessed port would be 5432 (you could probably just use PGPORT as your variable).
Also elsewhere on travis, it says local host will bind to 127.0.0.1 for other databases (eg. mongo) so I took a guess on that too.
Travis docs tell us the user as postgres and password is blank.
I was using the whole string to connect, where as you split it up, so as such I set my entire connection string as (you can extract each component):
postgres://postgres#127.0.0.1:5432/testing_db
I did this on the travis dashboard settings.
Amazingly all of this worked :tada:
Hope this helps.
As per docs, you can create a database using before_scriptlike so
before_script:
- psql -c "CREATE DATABASE testing_db;" -U postgres
then you can add your database credentials (along with database name and port etc) to travis environment variables like so
travis encrypt DB_USER=TEST_DB_USER --add env.matrix
You can find more on travis environment variables here. You can also add environment variables on travis dashboard under your repository settings.

Setting long environmental variables in nodejs

On a Node server that will be deployed to heroku, I have to load a long private key from the environmental variables. When I declare my environmental variable in a .env file in the traditional method (key=value, with no linebreaks if value is long), run it locally (through 'heroku local') and console.log the loaded environmental variable value, the console shows it has been split into several lines (but not around the '\n' character).
Loading the string from environmental variable:
Loading the string when it's hardcoded into the server and console logging shows:
I switched to foreman to load environmental variables and used the JSON syntax to get it working. My question is, on the traditional method, why does the console show the string is split up?
Loading the long value as this value is required for Firebase 3 SDK initialisation, as Heroku does not allow you to load the private key file itself.
For local development, the solution is to start the server using Node-Foreman and load the environmental variables from the .env file through the node-foreman utility. Use the JSON format for the environmental variables (not the key=value format). The JSON usage is described in the documentation.

Resources