对象#< MongoClient>没有方法'开放' [英] Object #<MongoClient> has no method 'open'

查看:71
本文介绍了对象#< MongoClient>没有方法'开放'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Node.js,Express.js和MongoDB创建一个简单的站点。我是这些技术的新手,并且一直有设置数据库的问题
这是我的index.js文件中的代码片段:

I've been trying to make a simple site with Node.js, Express.js, and MongoDB. I'm new to these technologies and have been having problem set up the database Here is snippet of code in my index.js file:

var http = require('http'),
    express = require('express'),
    path = require('path'),
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    CollectionDriver = require('./collectionDriver').CollectionDriver;

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

var mongoHost = 'localHost';
var mongoPort = 27017;
var collectionDriver;

var mongoClient = new MongoClient(new Server(mongoHost, mongoPort));
mongoClient.open(function(err, mongoClient) {
  if (!mongoClient) {
      console.error("Error! Exiting... Must start MongoDB first");
      process.exit(1);
  }
  var db = mongoClient.db("MyDatabase");
  collectionDriver = new CollectionDriver(db);
});

我尝试运行节点index.js 在终端中,它说如下:

After I try to run node index.js in terminal, it says the following:

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

/Users/username/dev/ga-final/index.js:31
mongoClient.open(function(err, mongoClient) { //C
            ^
TypeError: Object #<MongoClient> has no method 'open'
at Object.<anonymous> (/Users/username/dev/ga-final/index.js:31:13)
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:906:3

有什么问题?为什么可以我打电话给你吗?你能帮我解决这个问题吗?谢谢!

What is wrong? Why can't I call open? Can you help me fix this? thanks!

推荐答案

这种情况可能是因为你正在使用新的m的版本ongodb使用mongodb驱动1.4版后工作正常。

This is happening may be because you are using new version of mongodb it is working fine after I use mongodb driver version 1.4.

npm install mongodb@1.4.x

这篇关于对象#&lt; MongoClient&gt;没有方法'开放'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆