0

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!

2 Answers 2

1

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)
//   })
})
Sign up to request clarification or add additional context in comments.

4 Comments

now i am connection mongoose and the error is shown.
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/animals',{ useNewUrlParser: true }) mongoose.connection.once('open',()=> console.log('CONNECTED')) .on('error',(err)=>{ console.log(could not connect,err) });
and the error is could not connect { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Can you start mongodb?
1

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

3 Comments

bro now MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] error is showing. @Shubham Tiwari
sudo service mongod status try this command see what is the errorr showing in mongod
thank you server was inactive and i am make server active by sudo service mongod start. thank you bro!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.