0

i just finished learning how to implement crud using mangodb and am trying to connect node.js to mongodb. when i use 'mongodb://localhost:27017' as my url m terminal show an error (in the picture.) and when i use 'mongodb://127.0.0.1:27017'as my url, the terminal just hook please how can i solve this. The first image is where the terminal refuse to responsed image with terminal hook The second image is where the terminal showed an error image with error

well with my code i was expecting Connected to MongoDB server... in my console

1 Answers1

0

I hope this answer will help you to find a solution.

const mongoose = require('mongoose');

// mongoose.connect('mongodb://username:password@localhost:27017/your db name')
mongoose.connect('mongodb://127.0.0.1:27017/student')
.then(()=> console.log('Connected to DB'))
.catch((err)=> console.log(err));
  1. We need to make sure the Mongo db is running in localhost(if we are connecting with localhost)
  2. If we have the auth in your mongo db, Then we need to add the username and password during the DB connection.
suba
  • 1,320
  • 15
  • 22
  • Answer is not complete, see https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jun 03 '23 at 10:15