javascript - Mysql and node async.waterfall -
i trying find out if record exists in database table. if not exist there, want add database. use node , using async.waterfall
. wrong code , not find what. not add record database. suggestions?
var mysql = require('mysql'); var connection = mysql.createconnection({ host : 'localhost', user : 'user', password : 'password', database : 'database' }); (var m = 0; m <= (urls.length-1); m++) { var myurl = urls[m]; var thisname= thenames[m]; //iife function. omitted error handling clarity. (function(myurl,thisname){ async.waterfall([ //first find out if record exists in mydata function(next){ connection.query('select * mydata urllink=? limit 1',[myurl],next) }, //if record not exist, put in mydata function(results,next){ if (results.length==false){ console.log("new thing!"); //do stuff here, request(myurl...) find tablename. var post = {uniqueurl:myurl,thingname:thisname,thename:tablename}; connection.query('insert mydata set ?', post,next); }; }; ], //final callback function(err, results) { connection.end(); }; ); })(myurl,thisname); };
edit
:
i got error suggested answer typeerror: object not function
, @ line return next()
in if statement
. in real code (when use suggested answer):
async.series([ //first function function(callback){ //some calculations... callback(); }, //next function function(callback){ var = 0; //loop async.whilst( function() { return <= thefooz.length-1; }, //the innercallback: function(innercallback){ //some calculations urls , thenames. async.foreachof(urls, function(myurl, m, eachdone) { var thisname = thenames[m]; async.waterfall([ function(next) { connection.query( 'select * mydata urllink=? limit 1', [ myurl ], next ); }, function(results, next) { if (results.length !== 0) { return next(); } console.log("new thing!"); //do stuff here, request(myurl...) find tablename. var post = { uniqueurl : myurl, thingname : thisname, thename : tablename }; connection.query('insert mydata set ?', post, next); } ], eachdone); }, function(err) { if (err) throw err; // or handle errors connection.end(); }); settimeout(function() { i++; innercallback(); }, 10000); }); //close calculations in innercallback. }, //close innercallback. ); //close asyns.whilst callback; } //close function(callback) ], function(error){ if (error) return next(error); });
i stuck in callback hell
...
an (untested) version should same, bugfixes , enhancements in terms of leveraging more of async
's goodness:
var mysql = require('mysql'); var connection = mysql.createconnection({ host : 'localhost', user : 'user', password : 'password', database : 'database' }); // process url's in parallel (at same time). if process them // sequentially (one after another), can use `async.foreachofseries()` // instead. or, if want limit number of parallel requests, use // `async.foreachoflimit()`. async.foreachof(urls, function(myurl, m, eachdone) { var thisname = thenames[m]; async.waterfall([ function(next) { connection.query( 'select * mydata urllink=? limit 1', [ myurl ], next ); }, function(results, fields, next) { if (results.length !== 0) { return next(); } console.log("new thing!"); //do stuff here, request(myurl...) find tablename. var post = { uniqueurl : myurl, thingname : thisname, thename : tablename }; connection.query('insert mydata set ?', post, next); } ], eachdone); }, function(err) { if (err) throw err; // or handle errors connection.end(); });
instead of using foreach*
, can use each*
, since need array index foreach*
more convenient.
relevant documentation:
Comments
Post a Comment