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 v21.2.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: