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

  • Home
  • SEARCH
  • 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 7417701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:44:55+00:00 2026-05-29T07:44:55+00:00

I can’t figure out why the fetch function is not returning the information I

  • 0

I can’t figure out why the fetch function is not returning the information I wish to have:

(function(){
    var root = this;
    var Database = root.Database = {};

    var Db = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server = require('mongodb').Server;

    var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
    var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;

    // Require Underscore, if we're on the server, and it's not already present.
    var _ = root._;
    if (!_ && (typeof require !== 'undefined')) _ = require('./underscore');



    Database.ActiveRecord = function(attributes, collection){
        this.collection = collection;
        this.cid = _.uniqueId('c');
        attributes || (attributes = {});
        this.attributes = attributes;
    };

    // Connecting to database and setting the connection as a shared variable among ActiveRecords
    console.log("Connecting to " + host + ":" + port);

    Database.ActiveRecord.prototype.db = new Db('node-mongo-eslip', new Server(host, port, {}));

    _.extend(Database.ActiveRecord.prototype, {

        initialize: function(){},

        // Returns `true` if the attribute contains a value that is not null
        // or undefined.
        has: function(attr) {
            return this.attributes[attr] != null;
        },

        // Sets attributes
        set: function(key, value){
            var attrs, attr, val;
            if(_.isObject(key) || key == null){
                throw Error("The key should not be an object or null");
            }else{
                attrs = {};
                attrs[key] = value;
            }

            if (!attrs) return this;

            var now = this.attributes;

            for(attr in attrs){
                val = attrs[attr];
                if(!_.isEqual(now[attr], val)) now[attr] = val;
            }
        },

        unset: function(key){
            return this.set(attr, null);
        },

        toJSON: function() {
            return _.clone(this.attributes);
        },

        fetch: function(query, fields, options){
            var record = this;
            record.db.open(function(err, db){
                if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
                db.collection(record.collection, function(err, collection){
                    console.log('Fetching...');
                    collection.find(query, fields, options).toArray(function(err, docs) {
                        return docs;
                    });
                });
            });
        },

        save: function(){
            var record = this;
            record.db.open(function(err, db){
                if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
                db.collection(record.collection, function(err, collection){
                    collection.insert(_.clone(record.attributes), {safe:true},
                    function(err, objects) {
                        if (err) console.warn(err.message);
                        if (err && err.message.indexOf('E11000 ') !== -1) {
                            console.log('This id has already been inserted into the database');
                        }
                    });
                });
                console.log('Saved!');
            });
        }
    });
}());

I’ve spend quite some time trying to figure out what is missing, and didn’t make it, maybe someone would have better odds of figuring out.

  • 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-05-29T07:44:56+00:00Added an answer on May 29, 2026 at 7:44 am

    By the time the callback function from which you’re trying to run return docs is being executed, the external fetch function has already returned — as @SLaks suggested, welcome to the wonderful world of asynchronous programming.

    Promises can be an excellent way for dealing with asynchronous code — they’re available in jQuery, Dojo, and other libraries and toolkits. Your fetch method could return a promise, and code that called the fetch method could react when the returned promise was “resolved”. Here’s what that looks like with Dojo:

        fetch: function(query, fields, options){
            var record = this, dfd = new dojo.Deferred;
    
            record.db.open(function(err, db){
                if(!(record.collection||(typeof record.collection === 'string'))) throw Error('You should define a name attribute, which represents the collection of the Database');
                db.collection(record.collection, function(err, collection){
                    console.log('Fetching...');
                    collection.find(query, fields, options).toArray(function(err, docs) {
                        dfd.resolve(docs);
                    });
                });
            });
    
            return dfd.promise;
        },
    

    Then, code that called fetch might look like this:

    myDB.fetch().then(function(docs) {
      // do whatever you need with the docs
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can you tell me what is the difference between abstraction and information hiding in
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of
can someone explain why the compiler accepts only this code template<typename L, size_t offset,

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.