【问题标题】:Error in mongodb export in node,js节点,js中的mongodb导出错误
【发布时间】:2017-04-03 18:03:39
【问题描述】:

我试图使用 node.js 连接 mongodb。我偶然发现了一个问题。 我得到 require(...).pure is not a function error 。我尝试使用此站点“https://mongodb.github.io/node-mongodb-native/api-generated/collection.html”中的确切代码 我的代码如下。

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();
});

请告诉我你的建议。

【问题讨论】:

  • 题外话,但我建议你使用猫鼬
  • 你在哪里使用Db

标签: javascript node.js mongodb


【解决方案1】:
  1. 该文档没有编写好也没有经过测试,pure()mongodb-core 模块中

你应该安装mongodb-core并使用:

BSON = require('mongodb-core').BSON;
  1. 我可以看到您(以及该文档)根本没有使用 BSON,您应该删除它。

【讨论】:

  • require('mongodb-core').BSON 有效,但 require('mongodb-core').BSON.pure().BSON 给出相同的错误。是否只需要'require('mongodb-core').BSON'?
  • 就像我在第二点说的,你没有使用任何BSON 函数,只需删除需要行,你应该没问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2019-10-27
  • 2022-12-10
  • 2018-06-18
  • 2020-08-14
  • 2021-01-31
  • 2014-08-30
相关资源
最近更新 更多