NodeJS和node-mongodb-native

刚开始使用节点,并试图让mongo驱动程序工作。 我已经build立了连接,奇怪的是我可以插入的东西很好,但是调用find一个集合产生疯狂。

var db = new mongo.Db('things', new mongo.Server('192.168.2.6',mongo.Connection.DEFAULT_PORT, {}), {}); db.open(function(err, db) { db.collection('things', function(err, collection) { // collection.insert(row); collection.find({}, null, function(err, cursor) { cursor.each(function(err, doc) { sys.puts(sys.inspect(doc,true)); }); }); }); }); 

如果我取消注释插入和注释查找,它是一种享受。 反过来,不幸的是,我收到这个错误:

  collection.find({}, null, function(err, cursor) { ^ TypeError: Cannot call method 'find' of null 

我确定我在做一些愚蠢的事情,但是对于我的生活我找不到它…

我刚刚得到了同样的东西。 我意识到,由于某种原因,db.collection一遍又一遍地被调用,所以我做了这样的事情(对代码进行攻击):

  var db = new mongo.Db('things', new mongo.Server('192.168.2.6',mongo.Connection.DEFAULT_PORT, {}), {}); var Things; db.open(function(err, db) { db.collection('things', function(err, collection) { Things = Things || collection; }); var findThings = function() { Things.find({}, null, function(err, cursor) { cursor.each(function(err, doc) { sys.puts(sys.inspect(doc,true)); }); }); } 

我意识到你在9个月前问过这个问题。 希望这个严重的挖掘仍然有助于某人。 祝你好运!

尝试在插入之后调用collection.save()以刷新行。

看看http://www.learnboost.com/mongoose/

“目前Mongoose只支持手动刷新数据到服务器。”