Error trying to run node index.js

The error I keep getting when I run node index.js keeps referring to this line of code in mongoose.connect:

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

I looked at my entry in index.js and nothing has changed from before:
// b) Connect to Hosted DB
mongoose.connect(process.env.CONNECTION_URI, { useNewUrlParser: true, useUnifiedTopology: true });

This is the full error message I’m getting when I try to run node index.js:

MongooseError: 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.
at NativeConnection.Connection.openUri (C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\connection.js:689:11)
at C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\index.js:342:10
at C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\index.js:1176:10)
at Mongoose.connect (C:\Users\Owner\Documents\careerfoundry\myFlix-App\node_modules\mongoose\lib\index.js:341:20)
at Object. (C:\Users\Owner\Documents\careerfoundry\myFlix-App\index.js:56:12)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1155:10)
PS C:\Users\Owner\Documents\careerfoundry\myFlix-App>

I never had this problem before with connecting to the mongoose database. Can you help me out with this please?

It looks like this environment variable is missing.

This is in order to hide the actual connection URI in my index.js file for security. I know the connection between heroku and the mongoDB Atlas is working as I just clicked on “open app” in Heroku and I’m getting the “Welcome to myFlix movies club!” intro message from index.js.

So the problem appears to be somewhere with the node index.js command in relation to the code “mongoose.connect(process.env.CONNECTION_URI, { useNewUrlParser: true, useUnifiedTopology: true });”

I know that the line of code is working as I just mentioned that the connection between Heroku and MongDB Atlas is working. It’s also picking up the code from index.js

line 64: app.get(’/’, (req, res) => {
res.send(‘Welcome to myFlix movies club!’)

as well as MongoDB Atlas is still populated with the data from Mongo db, I just checked this, so right now I’m not clear on what this error message is telling me when I try to run node index.js.

If you’ve verified that the environment variable is where it’s supposed to be, then my best guess is that wherever you’re running index.js from doesn’t have access to it. You might need to create a local development .env file (that obviously doesn’t get committed) to run locally.

Here is the directory structure to show that it is included as part of this directory.

Here is a screen shot of the code from index.js and the error message in the terminal

If you’re running locally and all your environment variables are on Heroku, then anything that references process.env is going to have a problem.

Can you please explain what exactly I need to do to correct this?

Thank you

I can’t tell you in detail what changes you need to make to your local environment. Broadly speaking, you would probably have a local .env that has environment variables that may need to be the same or different from the production ones, depending on the situation. You would typically have a script or command that starts your application in “dev mode” or “local mode” which includes referencing the local environment file. Exactly how to do that is going to depend on your application, your build tools, and your environment.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.