0

so I'm new to MERN Stack and I'm trying to build a booking application wherein I'm using MongoDB Atlas for my database. I'm following a youtube tutorial to learn.

I am trying to connect my index.js file to .env file . I have used the connection string from mongodb atlas to connect but I keep getting this error.

TypeError: Cannot read properties of undefined (reading 'MONGO_URL') at Object. (C:\Users\Rashmika Satish\airbnbclone\api\index.js:20:29)
at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47

These are the respective files:

index.js file:

const express= require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const bcrypt=require('bcryptjs');
const User=require('./models/User.js');
require('dotenv').config();

const app=express();

const bcryptSalt=bcrypt.genSalt(10);

app.use(express.json());
app.use(cors({ 
      credentials:true,
      origin:'http://localhost:5173',
}))


console.log(process.env)
mongoose.connect(process.ev.MONGO_URL);

app.get('/test', (req,res)=> {
res.json('test ok');
});

app.post('/register', (req,res)=>{
    const {name,email,password}=req.body;
res.json({name,email,password});


   
    
    
});


app.listen(4000);

This is the .env file

MONGO_URL=mongodb+srv://*********:<password>@cluster0.1isyt6d.mongodb.net/?retryWrites=true&w=majority

(hidden the username and pw on purpose)

4
  • Can you see correct env object after console.log(process.env)? It must be {MONGO_URL: xxx} Commented Aug 16, 2023 at 8:50
  • Now I'm getting an error like this: C:\Users\Rashmika Satish\node_modules\mongodb\lib\cmap\connection.js:202 callback(new error_1.MongoServerError(document)); ^ MongoServerError: bad auth : authentication failed Commented Aug 16, 2023 at 8:55
  • You made a mistake in code. Try change ev to env mongoose.connect(process.ev.MONGO_URL); to mongoose.connect(process.env.MONGO_URL); Commented Aug 16, 2023 at 9:11
  • What does this error mean? throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'); ^ Commented Aug 16, 2023 at 9:16

2 Answers 2

0

change:

mongoose.connect(process.ev.MONGO_URL);

to:

mongoose.connect(process.env.MONGO_URL);
Sign up to request clarification or add additional context in comments.

5 Comments

Now I'm getting an error like this: C:\Users\Rashmika Satish\node_modules\mongodb\lib\cmap\connection.js:202 callback(new error_1.MongoServerError(document)); ^ MongoServerError: bad auth : authentication failed
Well, you see the message yourself, it's wrong username or password. I see your .env that you didn't change the <password> there
Thank you!! I solved it but i have another erro: throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'); ^
well you can try to console.log(process.env.MONGO_URL) and see if it's start with mongodb:// or mongodb+srv:// or not
I cannot cast an upvote since I don't have enough reputation points
0

There seems to be a syntactical error on line 20, process.ev.MONGO_URL should be process.env.MONGO_URL.

5 Comments

Now I'm getting an error like this: C:\Users\Rashmika Satish\node_modules\mongodb\lib\cmap\connection.js:202 callback(new error_1.MongoServerError(document)); ^ MongoServerError: bad auth : authentication failed
Now it tries to connect but it fails due to credentials being faulty. You need to edit the env variable MONGO_URL and pass in the correct credentials.
Oh the username and password is corrected but i got this error: throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"'); ^
Something is wrong with the url scheme, try replacing the mongodb+srv:// in MONGO_URL with mongodb:// to see if it makes any difference.
@RashmikaSatish Also try enclosing your env var in double quotes like this, MONGO_URL="mongodb+srv://*********:<password>@cluster0.1isyt6d.mongodb.net/?retryWrites=true&w=majority". Refer to this SO Question for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.