node.js mongodb - collection.find().toArray(callba

2020-07-03 08:35发布

问题:

I am just starting out with mongodb, but I am running into a problem when trying to use .find() on a collection.

I've created a DataAccessObject which opens a specific databate and then lets your perform operations on it. Here is the code:

The constructor:

var DataAccessObject = function(db_name, host, port){
    this.db = new Db(db_name, new Server(host, port, {auto_reconnect: true}, {}));
    this.db.open(function(){});
}

A getCollection function:

DataAccessObject.prototype.getCollection = function(collection_name, callback) {
    this.db.collection(collection_name, function(error, collection) {
        if(error) callback(error);
        else callback(null, collection);
  });
};

A save function:

DataAccessObject.prototype.save = function(collection_name, data, callback){
    this.getCollection(collection_name, function(error, collection){
        if(error) callback(error);
        else{
            //in case it's just one article and not an array of articles
            if(typeof (data.length) === 'undefined'){
                data = [data];
            }

            //insert to collection
            collection.insert(data, function(){
                callback(null, data);
            });
        }
    });
}

And what seems to be the problematic one - a findAll function:

DataAccessObject.prototype.findAll = function(collection_name, callback) {
    this.getCollection(collection_name, function(error, collection) {
      if(error) callback(error)
      else {
        collection.find().toArray(function(error, results){
            if(error) callback(error);
            else callback(null, results);
        });
      }
    });
};

Whenever I try to dao.findAll(error, callback), the callback never gets called. I've narrowed the problem down to the following part of the code:

collection.find().toArray(function(error, result){
    //... whatever is in here never gets executed
});

I've looked at how other people do it. In fact, I'm following this tutorial very closely. No one else seems to have this problem with colelction.find().toArray(), and it doesn't come up in my searches.

Thanks, Xaan.

回答1:

You are not using the open callback so if you are trying to make the findall request right after creating the dao then it won't be ready.

If your code is like this, it will not work.

var dao = new DataAccessObject("my_dbase", "localhost", 27017);

dao.findAll("my_collection",function() {console.log(arguments);});

I tested it and it doesn't find records, and it also gives no error. I think it should give an error.

But if you change it so that you give a callback to the constructor, then it should work.

var DataAccessObject = function(db_name, host, port, callback){
    this.db = new Db(db_name, new Server(host, port, {auto_reconnect: true}, {}));
    this.db.open(callback);
}

And make your code like this.

var dao = new DataAccessObject("my_dbase", "localhost", 27017, function() {
    dao.findAll("my_collection",function() {console.log(arguments);});
});