Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:46:54+00:00 2026-06-10T05:46:54+00:00

I am just starting out with mongodb, but I am running into a problem

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:46:55+00:00Added an answer on June 10, 2026 at 5:46 am

    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);});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting out with Netbeans and I ran into a problem trying to
Just starting out using MVC3 and hit a problem trying to build a drop-down
Hey - just starting out trying to get some openGL into my iphone app
Just starting out, this should be a simple one but I haven't been able
I'm just starting out using Lua, and I was wondering (because I can't find
I'm just starting out with Ruby/Rails and am wondering what Rails developers use to
Just starting out with my first commercial app for the iStore but dont have
I'm just starting out with Java, but my boss is pushing for this. I've
I'm just starting out with jQuery and want to use $(document).ready() to dynamically generate
I am just starting out with ajax...trying to submit a form with ajax through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.