I am using Digital Ocean’s app platform to host a NodeJS app. I do not understand the documentation for using environmental variables within my NodeJS code. https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables when I try to add the environmental variable like I think the documentation wants me to I get a syntax error: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true});
What is the correct usage?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
👋🏼 @jak6jak
The “bindable environment variables” are available for use in the app config. You’ll still need to add the actual environment variables to your app/component config, but you can use the templates as part of the values.
For example, if you have an app with a Node.js Service component and a database component, you can add an environment variable to the service component with:
DATABASE_URL
${db.DATABASE_URL}
${db.DATABASE_URL}
will be replaced by the database connection string before it is passed to the Node.js app. Then, in your code, you can use it just like a regular environment variable like so:
mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true});
The db
in ${db.DATABASE_URL}
must match the database component’s name, so if it’s different, you’ll need to replace it. You can find the component’s name in the control panel under Apps -> Components -> Info.
If you haven’t connected a database to your app yet, see How to Manage Databases in App Platform.
Click below to sign up and get $100 of credit to try our products over 60 days!