4

I was trying to connect mongodb using node.js. I stumbled on a problem. I am getting require(...).pure is not a function error . I tried to use the exact code from this site "https://mongodb.github.io/node-mongodb-native/api-generated/collection.html" My code is given below.

var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');

var uri = "mongodb://sa:psvitagamer5@cluster0-shard-00-00-
 tpfog.mongodb.net:27017,cluster0-shard-00-01-
  tpfog.mongodb.net:27017,cluster0-shard-00-02-
  tpfog.mongodb.net:27017/myNodeDb?ssl=true&replicaSet=Cluster0-shard-
  0&authSource=admin";


 MongoClient.connect(uri, function(err, db) {

   var collection = db.collection("userCollection");

    collection.insert({hello:'world_no_safe'});

   // Wait for a second before finishing up, to ensure we have written 
     the item to disk
   setTimeout(function() {

  // Fetch the document
   collection.findOne({hello:'world_no_safe'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_no_safe', item.hello);
      db.close();
   })
   }, 100);
  //db.close();
});

Please let me know your suggestions.

2
  • Out of topic but I suggest you to use mongoose Commented Apr 3, 2017 at 10:10
  • Where do you use Db ? Commented Sep 20, 2017 at 13:41

1 Answer 1

3
  1. That documentation is not well written nor tested, pure() is in mongodb-core module

You should install mongodb-core and use:

BSON = require('mongodb-core').BSON;
  1. As I can see you (and that documentation) are not using BSON at all, you should remove that.
Sign up to request clarification or add additional context in comments.

2 Comments

require('mongodb-core').BSON works but require('mongodb-core').BSON.pure().BSON gives the same error . Is 'require('mongodb-core').BSON' only required?
Like I said in second point, you are not using any BSON functions, just remove that require line and you should be ok

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.