amarjeet singh
amarjeet singh

Reputation: 165

facing problem in connecting mongoDB using the mongoDB client

i am connecting mongodb using the mongodb client in my app.

'app.js' file.

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', function (err, db) {
  if (err) throw err

//   db.collection('mammals').find().toArray(function (err, result) {
//     if (err) throw err

//     console.log(result)
//   })
})

the issue is:

(node:16348) DeprecationWarning: current URL string parser is 
deprecated, and will be removed in a future version. To use the new 
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/amarjeet/Desktop/node2/node_modules/mongodb/lib/operations/mongo_client_ops.js:474
      throw err;
       ^

MongoNetworkError: failed to connect to server [localhost:27017] on 
first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/home/amarjeet/Desktop/node2/node_modules/mongodb- 
core/lib/topologies/server.js:564:11)
    at Pool.emit (events.js:188:13)
    at Connection.<anonymous> 
(/home/amarjeet/Desktop/node2/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:276:13)
    at Connection.emit (events.js:188:13)
    at Socket.<anonymous> (/home/ amarjeet/Desktop/node2/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:276:13)
    at Socket.emit (events.js:188:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
[nodemon] app crashed - waiting for file changes before starting...

so this is the issue is shown on terminal and i have no idea how to fix it!

Upvotes: 0

Views: 652

Answers (2)

Shubham Tiwari
Shubham Tiwari

Reputation: 2041

You have to install MongoDB database server first in your system and start it.
if already installed:
check whether server is in start state. and Try connecting with mongo shell
and if server is also in start state:
than just put {useNewUrlParser: true } as mentioned by the @Vaghani Janak

Upvotes: 1

Vaghani Janak
Vaghani Janak

Reputation: 611

just add {useNewUrlParser: true } in connection

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', {useNewUrlParser: true }, function (err, db) {
  if (err) throw err
//   db.collection('mammals').find().toArray(function (err, result) {
//     if (err) throw err

//     console.log(result)
//   })
})

Upvotes: 1

Related Questions