Error connecting to the MongoDB database

Error connecting to the MongoDB database
0
#1

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:[email protected]/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)

#2
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’?

#3

It is module.exports not module.export

You have:

module.export = {
mongoURI:‘mongodb+srv://xxxusername:[email protected]/mdbtest?retryWrites=true’
}
2 Likes
#4

Thanks, here is my mistake

#5

Problem solved, thank you

1 Like
#6

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.

1 Like
#7

Благодарю за помощь!)