1

I Have this Code saved as mongodb-connect.js

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
    if(err){
        return console.log('Unable to connect to mongo db server');
    }
    console.log('Connected to Mongo DB server');

    db.collection('Todos').insertOne({
      text: 'Something to do',
      completed: false 
    }, (err, result) => {
        if(err) {
            return console.log('Unable to insert todo',err);
        }
        console.log(JSON.stringify(result.ops, undefined, 2));
    });

    db.close();
});

but its showing:

(node:7720) 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.
Connected to Mongo DB server
/home/ghoul/node-todo-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:439
      throw err;      ^
TypeError: db.collection is not a function
    at MongoClient.connect (/home/ghoul/node-todo-api/playground/mongodb-connect.js:9:8)
    at result (/home/ghoul/node-todo-api/node_modules/mongodb/lib/utils.js:414:17)
    at executeCallback (/home/ghoul/node-todo-api/node_modules/mongodb/lib/utils.js:40
6:9)
    at err (/home/ghoul/node-todo-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:285:5)
    at connectCallback (/home/ghoul/node-todo-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:240:5)
    at process.nextTick (/home/ghoul/node-todo-api/node_modules/mongodb/lib/operations/mongo_client_ops.js:436:7)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

1 Answer 1

2

Pass option { useNewUrlParser: true } to the connect method as stated by documentation or use:

cons url = 'your url';
const dbName = 'myproject';
MongoClient.connect(url, function(err, client) {
      const db = client.db(dbName);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Should be const url = 'your url';

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.