Node.js和Native MongoDB连接失败

时间:2013-01-31 21:14:20

标签: node.js mongodb

我正在尝试在app.js节点应用程序中设置一个简单的mongodb测试服务器,但我一直得到“TypeError:无法读取未定义的属性'arbiterOnly'”。我在本地主机上运行它并且我已经通过在我正在创建应用程序的文件夹中运行npm install mongodb来安装mongo db。任何帮助我正在做错的将是非常苛刻的

这是我的应用程序代码

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');
  var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var client = new Db('test', new Server('localhost:', 3100, {}));

var insertData = function(err, collection) {
    collection.insert({name: "Kristiono Setyadi"});
    collection.insert({name: "Meghan Gill"});
    collection.insert({name: "Spiderman"});
    // you can add as many object as you want into the database
}

var removeData = function(err, collection) {
    collection.remove({name: "Spiderman"});
}

var updateData = function(err, collection) {
    collection.update({name: "Kristiono Setyadi"}, {name: "Kristiono Setyadi", sex: "Male"});
}

var listAllData = function(err, collection) {
    collection.find().toArray(function(err, results) {
        console.log(results);
    });
}

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});


client.open(function(err, pClient) {
    client.collection('test_insert', insertData);
  //  client.collection('test_insert', removeData);
    // etc.
});

var people = [{name:'Keth',age:'33',email:'ktater@gmail.com'},
{name:'Donny',age:'20',email:'donjuan86@hotmail.com'},
{name:'Loran',age:'26',email:'geegeenat@facebook.com'},
{name:'Max',age:'18',email:'axxanan@gmail.com'}];

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/people', function(req, res){
  res.render('peeps', {people:people});
});

app.get('/people/:id', function(req, res){
var guy;
  for (var i =0 ; i < people.length ; i++)
  {
    if(people[i].name == req.params.id)
      guy = people[i];

  }
  res.render('viewPerson', {guy:guy});
});

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

1 个答案:

答案 0 :(得分:2)

我的第一个猜测是连接没有打开。尝试在client.open函数中使用err记录console.log。此外,您从中获取测试代码的帖子大约是18个月,代码可能已过期。