hiteshchoudhary / chai-backend

A video series on chai aur code youtube channel
5.06k stars 760 forks source link

Error in connecting to DB after creating my first route. #122

Closed Harshitk816 closed 4 months ago

Harshitk816 commented 5 months ago

So I am in the video of 'ROUTES AND CONTROLLER WITH DEBUGGING". After the creating my register route, i started the server and got: `Server is running on port 8000 /n Database connected !! DB HOST: ac-fdtffkb-shard-00-02.gcq7rur.mongodb.net Error in DB Connection TypeError: Cannot read properties of undefined (reading 'method') at Function.handle (C:\Users\Harshit\Desktop\Chai Aur Code Backend\chai_aur_backend\node_modules\express\lib\router\index.js:139:34)
at Function.handle (C:\Users\Harshit\Desktop\Chai Aur Code Backend\chai_aur_backend\node_modules\express\lib\application.js:181:10)
at app (C:\Users\Harshit\Desktop\Chai Aur Code Backend\chai_aur_backend\node_modules\express\lib\express.js:39:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async file:///C:/Users/Harshit/Desktop/Chai%20Aur%20Code%20Backend/chai_aur_backend/src/index.js:15:1

`

I am unable to identify the problem yet. But it was connecting properly the last time I checked.. This is my index.js image

this is my db/index.js image

Nishantsingh11 commented 4 months ago

-Hi harshit i think you have created the method some where but its empty as of now can you please check the code

s0urav-hald3r commented 4 months ago

While configuring the env file you write the path wrong. It should be path: './.env'

Harshitk816 commented 4 months ago

it was a simple fix i forgot to add a callback brackets to the error listening in the index.js:

connectDB() .then(()=>{ app.on("error",(error)=>{ console.log("Error in Database Connection : " + error); }) } previously it was : connectDB() .then( app.on("error",(error)=>{ console.log("Error in Database Connection : " + error); ) }