Mongoose URI is undefined

Tell us what’s happening:

I was following freecodecamp’s backend course, and i stumbled upon some errors with the URI variable. I found the solution here on this forum; i just needed to delete the sample.env and create my own .env file. But it’s just that i can’t create a file without selecting a folder. So i created it in views,
but it gave me another error

The Error
It’s essentially that it’s getting undefined instead of a string for the URI variable, but i prefered the put the whole thing here.

Your app is listening on port 3000
(node:154) UnhandledPromiseRejectionWarning: 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 (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:694:11)
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:351:10
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:350:20)
at Object. (/home/runner/boilerplate-mongomongoose-1/myApp.js:3:10)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/home/runner/boilerplate-mongomongoose-1/server.js:67:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
(node:154) UnhandledPromiseRejectionWarning: Unhandled promise
 npm start

fcc-mongo-mongoose-challenges@0.0.1 start /home/runner/boilerplate-mongomongoose-1
node server.js

Your app is listening on port 3000
(node:154) UnhandledPromiseRejectionWarning: 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 (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:694:11)
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:351:10
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:350:20)
at Object. (/home/runner/boilerplate-mongomongoose-1/myApp.js:3:10)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/home/runner/boilerplate-mongomongoose-1/server.js:67:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
(node:154) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see Command-line API | Node.js v17.8.0 Documentation). (rejection id: 1)
(node:154) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Your code so far
I left the default, and added:
In app.js:
const mongoose = require(‘mongoose’);
mongoose.connect(process.env.MONGO_URI);

In package.json, in dependencies, added:
“mongodb”: “^3.0.0”,
“mongoose”: “^5.13.14”
},

And in .env:
MONGO_URI=‘mongodb+srv://:@<Whatever is in there, i left it at default>.mongodb.net/myFirstDatabase?retryWrites=true&w=majority’

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Install and Set Up Mongoose

Link to the challenge:

You are using replit ?

Yes, and its the first time btw

Replit no longer support .env files instead you can see a lock icon saying secrets in the left sidebar that can be used to declare environment variables

OOOOHHH ok… So that should be updated
in the course… I’ll try it and see if it works.
Ok so idk if it worked… cuz it gave back that error:


(node:1605) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Your app is listening on port 3000
(node:1605) UnhandledPromiseRejectionWarning: Error: Invalid schema, expected mongodb or mongodb+srv
at module.exports (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:24:21)
at deprecated (internal/util.js:89:15)
at connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:283:3)
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:284:5
at maybePromise (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
at MongoClient.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:280:10)
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:836:12
at new Promise ()
at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:832:19)
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:351:10
at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:1149:10)
at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:350:20)
at Object. (/home/runner/boilerplate-mongomongoose-1/myApp.js:3:10)
(node:1605) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see Command-line API | Node.js v17.8.0 Documentation). (rejection id: 3)
(node:1605) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


I looked it up on the internet, seems the problem to be with my URI … maybe it shouldnt be mongodb+srv://… maybe that srv is something that i should fill?

  1. <Whatever is in there, i left it at default> the default doesn’t have the password only the username (looks something like this username:<password>)

  2. Replit adds quotes around the connection string automatically so you get double strings if you add the connection string with quotes. Click the secret button, then the “Open raw editor” button, check that the connection string only has one set of quotes (edit it as needed).

1 Like

2- It WAS the problem! I removed the quotes and it worked now! So am pretty sure that the URI is correct, because my username:password is in it. Appreciate the help sir :grin: :pray:!

1 Like