Ayanakoji_kiyotaka
Ayanakoji_kiyotaka

Reputation: 9

connect node.js to mongoDB

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

Upvotes: 0

Views: 42

Answers (1)

suba
suba

Reputation: 1440

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.

Upvotes: 0

Related Questions