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)

2 Answers2

0

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

vmank
  • 773
  • 2
  • 7
  • 22
  • 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 – Rashmika Satish Aug 16 '23 at 08:50
  • 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. – vmank Aug 16 '23 at 08:55
  • 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://"'); ^ – Rashmika Satish Aug 16 '23 at 09:14
  • Something is wrong with the url scheme, try replacing the `mongodb+srv://` in `MONGO_URL` with `mongodb://` to see if it makes any difference. – vmank Aug 16 '23 at 09:18
  • @RashmikaSatish Also try enclosing your env var in double quotes like this, `MONGO_URL="mongodb+srv://*********:@cluster0.1isyt6d.mongodb.net/?retryWrites=true&w=majority"`. Refer to this [SO Question](https://stackoverflow.com/a/71538763/6869922) for more details. – vmank Aug 16 '23 at 09:24
0

change:

mongoose.connect(process.ev.MONGO_URL);

to:

mongoose.connect(process.env.MONGO_URL);
cantdocpp
  • 350
  • 7
  • 18
  • 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 – Rashmika Satish Aug 16 '23 at 08:52
  • Well, you see the message yourself, it's wrong username or password. I see your ```.env``` that you didn't change the `````` there – cantdocpp Aug 16 '23 at 08:55
  • 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://"'); ^ – Rashmika Satish Aug 16 '23 at 09:13
  • well you can try to ```console.log(process.env.MONGO_URL)``` and see if it's start with ```mongodb://``` or ```mongodb+srv://``` or not – cantdocpp Aug 16 '23 at 09:20
  • I cannot cast an upvote since I don't have enough reputation points – Rashmika Satish Aug 22 '23 at 10:05
  • okay no problem, thank you – cantdocpp Aug 22 '23 at 10:06