MongoDB URI is not being recognized when connecting to database

Tell us what’s happening:
I’m getting this error when trying to start my node server on my replit.

/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:434
    throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
    ^
Error [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 new MongooseError (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/error/mongooseError.js:14:11)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:434:11)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/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.<anonymous> (/home/runner/boilerplate-mongomongoose/server.js:67:16)
    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 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fcc-mongo-mongoose-challenges@0.0.1 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the fcc-mongo-mongoose-challenges@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-05-22T00_42_18_462Z-debug.log
exit status 1

but I don’t understand why. In my .env secrets file my URI looks like this

MONGO_URI='mongodb+srv://<ACCOUNT>:<PASSWORD>@<DATABASE>/?retryWrites=true&w=majority'

with the correct account information

Your code so far
First I added MongoDB and mongoose to my package.json

"mongodb": "^3.6.0",
"mongoose": "^5.4.0"

I also set up my MongoDB atlas account and created my cluster as per the guide, then added these two lines to the top of myApp.js to connect my database

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

Any help would be greatly appreciated!

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36

Challenge: Install and Set Up Mongoose

Link to the challenge:

You can’t use a .env file with Replit.

https://docs.replit.com/programming-ide/storing-sensitive-information-environment-variables

Edit: you might also have to add the database name to the connection string.

1 Like

Thank you for the response! I forgot about how replit handles secrets and I’ve moved it to the environment variable section on the sidebar as well as added the db name to the string like you suggested, but I am now getting this error message instead

the options [useUnifiedTopology] is not supported
Your app is listening on port 3000
(node:4607) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:40:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose/node_modules/mongodb-core/lib/uri_parser.js:421:12)
    at connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:188:3)
    at connectOp (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:292:3)
    at executeOperation (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
    at /home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:527:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/connection.js:524:19)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose/node_modules/mongoose/lib/index.js:271:15)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose/myApp.js:4: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)
(node:4607) 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 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:4607) [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’m not sure what some of this means and it didn’t actually stop the project from running, but I’m worried about the possibility that it might affect some of the future challenges so I’d still like to know what’s going on.

EDIT: the node server doesn’t close but apparently something is happening that is preventing the last test on the challenge from passing and I’m not sure where to even begin to fix it.

I think that error usually comes from having the connection string inside quotes.

When you added the connection string did you have it inside quotes? If so go to the secret tab and click the Raw option and make sure the connection string is not inside double strings as Replit will automatically add the string around the value you add.

1 Like

This was the problem, thank you so much!

So in case anyone else comes across this and it helps, the steps were making sure my URI was in the secrets section for the replit, adding the database name to the connection string; so going from

MONGO_URI='mongodb+srv://<ACCOUNT>:<PASSWORD>@<DATABASE>/?retryWrites=true&w=majority'

to

MONGO_URI='mongodb+srv://<ACCOUNT>:<PASSWORD>@<DATABASE>/<DATABASE_NAME>?retryWrites=true&w=majority'

and finally removing the quotes around it since the replit adds them to the raw JSON.

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