Hey there, I'm diving into the world of MERN Stack and working on a booking application. Currently, I'm leveraging MongoDB Atlas for my database setup and following a tutorial on YouTube to grasp the concepts.
My current hurdle is connecting my index.js file to the .env file. I've input the connection string from MongoDB Atlas, but unfortunately, I'm encountering the following 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
Here are the relevant 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
(I've intentionally hidden the username and password for security purposes)