最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS and node-mongodb-native - Stack Overflow

programmeradmin12浏览0评论

Just getting started with node, and trying to get the mongo driver to work. I've got my connection set up, and oddly I can insert things just fine, however calling find on a collection produces craziness.

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

    });
});

If I unment the insert and ment out the find, it works a treat. The inverse unfortunately doesn't hold, I receive this error:

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

I'm sure I'm doing something silly, but for the life of me I can't find it...

Just getting started with node, and trying to get the mongo driver to work. I've got my connection set up, and oddly I can insert things just fine, however calling find on a collection produces craziness.

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

    });
});

If I unment the insert and ment out the find, it works a treat. The inverse unfortunately doesn't hold, I receive this error:

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

I'm sure I'm doing something silly, but for the life of me I can't find it...

Share Improve this question edited Oct 16, 2011 at 2:50 Farid Nouri Neshat 30.4k6 gold badges77 silver badges125 bronze badges asked Jun 17, 2010 at 18:37 user369680user369680 611 silver badge2 bronze badges 2
  • I think this means you get an error. Can you check the 'err' value? – Fopfong Commented Jun 18, 2010 at 5:21
  • Also, I think you use when you use new mongo.Db('things'... 'things' is DB name not a collection. I'm not sure that this cause the problem or not – Fopfong Commented Jun 18, 2010 at 5:27
Add a ment  | 

2 Answers 2

Reset to default 9

I got the same thing just now. I realized that db.collection is being called over and over again for some reason, so I did something like this (hacking away on your code):

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

I realize you asked this 9 months ago. Hope this grave diggin still helps someone. Good luck!

try to call collection.save() after your insert to flush your row.

take a look at http://www.learnboost./mongoose/

"Currently Mongoose only supports manual flushing of data to the server."

发布评论

评论列表(0)

  1. 暂无评论