Monday, 15 September 2014

node.js - NodeJS executing code in wrong order. Why? -



node.js - NodeJS executing code in wrong order. Why? -

the code i've written executing in blocks, rather sequentially. example:

task 1 result task 1 result task 1 result task 2 result task 2 result task 2 result task 3 result task 3 result task 3 result

i'd order of execution instead:

task 1 result task 2 result task 3 result task 1 result task 2 result task 3 result task 1 result task 2 result task 3 result

my code:

var async = require('async'); var fs = require('fs'); var mysql = require('mysql'); var connection = mysql.createconnection({ host : 'localhost', user : 'root', password : '', }); function getfiles(dir,files_){ files_ = files_ || []; if (typeof files_ === 'undefined') files_=[]; var files = fs.readdirsync(dir); for(var in files){ if (!files.hasownproperty(i)) continue; var _name = files[i]; var name = dir+'/'+files[i]; if (fs.statsync(name).isdirectory()){ getfiles(name,files_); } else { files_.push(_name); } } homecoming files_; } connection.connect(function(err) { if(err){ console.log('error: '+err); }else{ var directory = "products_json/13658/"; var all_files = getfiles(directory); async.eachseries(all_files, function( file, callback) { // skip iteration if file mac .ds_store file if(file !== ".ds_store"){ console.log('processing file ' + directory + file); fs.readfile(directory+file, function read(err, filedata){ var productsdata = json.parse(filedata); async.eachseries(productsdata.results, function( product, callback2) { async.eachseries(object.keys(product), function( productkey, callback3) { var checkifcolumnexistsquery = connection.query( "select * information_schema.columns table_schema = ? , table_name = ? , column_name = ? limit 0 , 30;", ['unisaver', 'products', productkey], function(err, rows, fields) { // create field if doesn't exist if(rows.length == 0){ console.log(productkey + " --- "+ typeof product[productkey] + " --- "+ product[productkey]); if( typeof product[productkey] === 'object' ){ var createcolumnquery = connection.query( "alter table ?? add together ?? text(1000);", ['unisaver.products', productkey] ); }else if( typeof product[productkey] === 'string' ){ var createcolumnquery = connection.query( "alter table ?? add together ?? varchar(255);", ['unisaver.products', productkey] ); }else if( typeof product[productkey] === 'number' ){ var createcolumnquery = connection.query( "alter table ?? add together ?? int(11);", ['unisaver.products', productkey] ); } } } ); callback3(); }); // todo: insert row callback2(); }); }); } callback(); }, function(err){ console.log('finished'); }); } });

the output is:

processing file products_json/13658/0.json processing file products_json/13658/1.json processing file products_json/13658/10.json cost ----: ..... cost ----: ..... cost ----: .....

this means database queries beingness executed after other work has been done. i'd queries run within loop , produce next output instead:

processing file products_json/13658/0.json cost ----: ..... processing file products_json/13658/1.json cost ----: ..... processing file products_json/13658/10.json cost ----: .....

what doing wrong? , how can prepare this? i'm stuck on problem :(

you're calling callbacks right away before asynchronous requests finished. also, need 1 async.eachseries() (where phone call callback when mysql query finished).

node.js asynchronous node-mysql

No comments:

Post a Comment