11

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 Answer 1

9

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);});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I should have kept that in mind. The tutorials I'm following seem to assume that any operations on such a DAO do not happen immediately after its creation. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.