Reputation: 9
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.
Upvotes: 0
Views: 113
Reputation: 25466
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).
Upvotes: 1