2

Here's what I have:

var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;

var app = express();

var client = new MongoClient(new Server('localhost', 27017, {}), {});

client.open(function(err, client){
//callback
});

And when I run it, it points to the line the open method is on and says "undefined is not a function". What am I doing wrong?

3
  • 1
    Did you follow the docs, does it say you should require the same module twice like that ? Commented Jun 26, 2015 at 10:56
  • What happens when you use client.connect() instead of client.open()? Commented Jun 26, 2015 at 10:59
  • 1
    Refer to the documentation for more guidelines. Commented Jun 26, 2015 at 10:59

1 Answer 1

5

From the mongo docs :

var MongoClient = require('mongodb').MongoClient

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  console.log("Connected correctly to server");

  db.close();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Figured out my problem. The book I was following instructions on was using 1.4. I had 2.0 installed.

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.