【问题标题】:Getting failed to load c++ bson extension error using Mongodb and Node.js使用 Mongodb 和 Node.js 无法加载 c++ bson 扩展错误
【发布时间】:2015-08-30 12:37:59
【问题描述】:

我在尝试使用 Node.ja 和 Mongodb 运行服务器时遇到以下错误。

错误:

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version

C:\xampp\htdocs\odiya_chat\server.js:15
db = mongo.connect("127.0.0.1:27017/"+db, collections);
           ^
TypeError: Object function (connString, cols) {
  var dbname = getDbName(connString);
  var onserver = thunky(function(cb) {
    getTopology(connString, function(err, topology) {
      if (err) return cb(err);
      cb(null, topology);
    });
  });

  if (!dbname) {
    dbname = connString._dbname;
    onserver = thunky(function(cb) {
      toMongodbCore(connString, function(err, server) {
        if (err) cb(new Error('You must pass a connection string or a mongojs in
stance.'));
        cb(null, server);
      });
    });
  }

  var that = new Database({name: dbname, cols: cols}, onserver);
  if (typeof Proxy !== 'undefined') {
    var p = Proxy.create({
      get: function(obj, prop) {
        if (that[prop]) return that[prop];
        that[prop] = that.collection(prop);
        return that[prop];
      }
    });

    return p;
  };
  return that;
} has no method 'connect'
    at Object.<anonymous> (C:\xampp\htdocs\odiya_chat\server.js:15:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

错误 2

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }

js-bson: Failed to load c++ bson extension, using pure JS version

我的服务器端代码如下。

服务器.js

var port=8888;
var express=require('express');
var http=require('http');
var morgan         = require('morgan');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var cookieParser     = require('cookie-parser');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);
var mongo = require('mongojs');
var app=express();
var server=http.createServer(app);
db = 'doctor',
collections = ['oditek'],
db = mongo.connect("127.0.0.1:27017/"+db, collections);
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
app.use(morgan('dev'));                     // log every request to the console
app.use(bodyParser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded
app.use(bodyParser.json())    // parse application/json
app.use(methodOverride());
app.use(cookieParser());
//app.use(expressSession({secret:'somesecrettokenhere'}));                  // simulate DELETE and PUT
app.get('/',function(req,res){
    res.sendfile('view/index.html');
})
app.get('/login',function(req,res){
    res.sendfile('view/login.html');
});
app.get('/chatroom',function(req,res){
    if(req.session){
        res.sendfile('view/chatroom.html');
    }
});
server.listen(port);
console.log('server is running on the port'+port);

我的要求是,如果用户已经登录客户端,他将获得 chatroom.html 页面,如果不在客户端,它将要求您登录。我在这里实现 connect-mongodb-session 以存储会话。我有阅读有关此的一些帖子并尝试了答案,但仍然存在错误。请帮助我解决此错误。

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    由于您使用的是mongojs 模块,因此您必须使用以下方法连接到数据库

    db = mongo("127.0.0.1:27017/"+db, collections);
    

    【讨论】:

    • @Bidhan:感谢它现在开始工作,但仍然存在一些错误,我已在我的问题中将这些错误发布为 Error-2。
    • 那些不是错误。只有警告。这意味着您使用的是速度较慢的 JS 版本,而不是使用速度快的 bson 版本。发生这种情况是由于您的模块的不同版本存在问题。不过,您的应用程序应该仍然可以正常运行。你的应用在运行吗?
    • 是的,Bidhan 它运行良好,但那些消息在那里,这就是我问的原因..有什么解决方案可以删除这些..?
    • 在 Stack Overflow 上已经有很多关于这些警告信息的讨论。例如:stackoverflow.com/questions/28651028/…。有的人能解决,有的人不行。它通常是由于您使用的不同版本的库之间的不匹配而发生的。也许切换到旧版本的 mongojs 可能会有所帮助,但我不是 100% 确定。
    猜你喜欢
    • 2014-03-06
    • 2015-08-09
    • 2014-06-05
    • 2015-08-27
    • 2015-01-24
    • 2015-06-17
    • 2015-03-06
    相关资源
    最近更新 更多