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:UsersRashmika Satishairbnbcloneapiindex.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
Answers
There seems to be a syntactical error on line 20,
process.ev.MONGO_URL
should beprocess.env.MONGO_URL
.change:
to: