Error exporting mongodb to node, js

I tried to connect mongodb using node.js. I came across a problem. I receive a request (...). 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 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.

+3


source to share


1 answer


  • This documentation is poorly written and not tested, pure()

    is in the modulemongodb-core

You must install mongodb-core

and use:



BSON = require('mongodb-core').BSON;

      

  1. As I see you (and this documentation) are not using at all BSON

    , you should remove that.
+2


source







All Articles