Error connecting to the MongoDB database

Hello! Help Wanted! Nodejs development environment. Connecting to the database in the Mongodb Atlass cloud
File: app.js

const express = require ('express') 
const mongoose = require ('mongoose')
 const path = require ('path')
 const keys = require ('./ keys')
 const port = process.env.PORT || 5000
 const clientPath = path.join (__ dirname, 'client') 
mongoose.connect (keys.mongoURI)
    .then (() => console.log ('MongoDB connected.')) 
   .catch (err => console.error (err)) 

const app = express () 
app.use (express.static (clientPath))
 app.listen (port, () => {  console.log (`The server started on the port $ {port}`) })


**File:keys.js**
module.export = {
mongoURI:'mongodb+srv://xxxusername:xxxpassword@cluster0-m3jmz.mongodb.net/mdbtest?retryWrites=true'
}

**An error is displayed in the terminal:**
C:\Users\user\Desktop\fullstack-blog\node_modules\mongoose\lib\connection.js:459
    throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
    ^
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.co
nnect()` or `mongoose.createConnection()` is a string.
    at new MongooseError (C:\Users\user\Desktop\fullstack-blog\node_modules\mongoose\lib\error\mongooseError.js:14:11)
    at NativeConnection.Connection.openUri (C:\Users\user\Desktop\fullstack-blog\node_modules\mongoose\lib\connection.js:459:11)
    at Mongoose.connect (C:\Users\user\Desktop\fullstack-blog\node_modules\mongoose\lib\index.js:289:15)
    at Object.<anonymous> (C:\Users\user\Desktop\fullstack-blog\app.js:9:10)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)

    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
[nodemon] app crashed - waiting for file changes before starting...

Please help who can. Iā€™m new, Iā€™m learning)

1 Like
C:\Users\user\Desktop\fullstack-blog\node_modules\mongoose\lib\connection.js:459
throw new MongooseError('The `uri` parameter to `openUri()` must be a ā€™ +
^
MongooseError: The `uri` parameter to `openUri()` must be a string, got ā€œundefinedā€. Make sure the first parameter to `mongoose.co nnect()` or `mongoose.createConnection()` is a string.

Based on this part of the error logs, have you checked if you are making the connection call correctly? Not necessarily the source of this error, but a good place to start.

And also this

const keys = require (ā€™./ keysā€™)

Can you check again if thereā€™s supposed to be a white space between ā€˜/ā€™ and ā€˜kā€™?

It is module.exports not module.export

You have:

module.export = {
mongoURI:ā€˜mongodb+srv://xxxusername:xxxpassword@cluster0-m3jmz.mongodb.net/mdbtest?retryWrites=trueā€™
}
3 Likes

Thanks, here is my mistake

Problem solved, thank you

1 Like

Iā€™ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The ā€œpreformatted textā€ tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Š‘Š»Š°Š³Š¾Š“Š°Ń€ŃŽ Š·Š° ŠæŠ¾Š¼Š¾Ń‰ŃŒ!)

thanks man this helped