0

I've been using javascript for years, and gotten pretty good at it since I started a web development job, but working with node on a personal project I am having issues.

I create an object

    ;( function( undefined ) {
    'use strict';

    var scrape = {
        data : {},
        scrapers: [],
        init : function( coins, exchanges, trade_paths ) {
            scrape.start_time = new Date().getMilliseconds();
            // Load events
            var Event = require( 'events' );
            // Add an eventEmitter to process
            process.event = new Event.EventEmitter()

            // Now we can load any modules, now that global process is modified ;)
            //require( './bter.js' );
            exchanges.forEach( function( exchange ) {
                console.log( exchange.name + '.js' );
                require( exchange.name.toLower() + '.js' );
            } );

            // Trigger the preload event
            process.event.emit( 'scraper::init', coins );

            // Response to all modules processes
            process.event.on( 'scraper::add', scrape.add );
        },
        add : function( module ) {
            scrape.data[module.name] = module.data;
        }
    };

    // Get list of exchanges, coins, and trade paths
    var sql_data = {
        sql : {},
        db : {},
        coins : [],
        exchanges : [],
        trade_paths : [],
        init : function() {
            sql_data.sql = require( 'mysql' );
            sql_data.db = sql_data.sql.createConnection( {
                host : '127.0.0.1',
                user : 'root',
                password : ''
            } );

            sql_data.db.connect();

            // Get coin list
            sql_data.db.query('SELECT * FROM `node`.`coins` WHERE active=true', function(err, rows, fields) {
                if( typeof rows !== 'undefined' ) {
                    sql_data.coins = rows;
                }
                // Oddly, if no error, its equal to null.
                if( err !== null ) {
                    console.log( err );
                }
            } );

            // Get exchange list
            sql_data.db.query( 'SELECT * FROM `node`.`exchanges` WHERE active=true', function( err, rows, fields ) {
                if( typeof rows !== 'undefined' ) {
                    sql_data.exchanges = rows;
                }
                if( err !== null ) {
                    console.log( err );
                }
            } );

            // Get trade paths
            sql_data.db.query( 'SELECT * FROM `node`.`trade_paths` WHERE active=true',     function( err, rows, fields ) {
                if( typeof rows !== 'undefined' ) {
                    sql_data.trade_paths = rows;
                }
                if( err !== null ) {
                    console.log( err );
                }
            } );

            // Close connection to the database
            sql_data.db.end();
        }
    };

    sql_data.init();

    // Start scraping
    scrape.init( sql_data.coins, sql_data.exchanges, sql_data.trade_paths );
} () );

object.x is not accessible. Not even within object itself. I don't know what to do or how to fix this.

6
  • Which version? I am getting [ 'a', 'b', 'c' ] Commented Jan 12, 2014 at 2:33
  • v0.10.22 on windows 8.1 Commented Jan 12, 2014 at 2:33
  • I'm getting [ 'a', 'b', 'c' ] on the exact same version. Are you doing something else weird? Commented Jan 12, 2014 at 2:35
  • Please check this ideone.com/SxeBCp It produces [ 'a', 'b', 'c' ] only Commented Jan 12, 2014 at 2:36
  • I get ['a','b','c'] on 10.7 on Mac. Maybe change to object.x=... to this.x = ['a','b','c'] on line 4? Commented Jan 12, 2014 at 2:41

1 Answer 1

1

Your scrape.init function is called before you received data from mysql. You need to call it inside sql_data.init callback (which you can safely call in third .query() since mysql queries are executed sequentially per connection).

Sign up to request clarification or add additional context in comments.

1 Comment

I very much appreciate it. I start my work within the third callback function from mysql and all is good. Makes perfect sense now that I understand what was going on.

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.