简体   繁体   中英

Mysql and node async.waterfall

I am trying to find out if the record exists in my database table. If it does not exist there, then I want to add it to my database. I use node and using async.waterfall . But something is wrong with my code and I could not find what. It does not add the record to my database. Any suggestions?

var mysql       = require('mysql');
var connection  = mysql.createConnection({
  host     : 'localhost',
  user     : 'user',
  password : 'password',
  database : 'database'
});


for (var m = 0; m <= (urls.length-1); m++) {
     var Myurl = urls[m];
     var ThisName= TheNames[m];

     //IIFE function. I omitted the error handling for clarity.

     (function(Myurl,ThisName){

        async.waterfall([

          //First find out if the record exists in mydata
          function(next){
          connection.query('SELECT * FROM mydata WHERE UrlLink=?   
                            LIMIT 1',[Myurl],next)
                        },

          //If the record does not exist, put it in mydata
          function(results,next){
          if (results.length==false){
              console.log("New Thing!");
              //Do some stuff here, request(Myurl...) to find TableName.
              var post  = {UniqueUrl:Myurl,ThingName:ThisName,TheName:TableName};
              connection.query('Insert INTO mydata Set ?', post,next);
                                    };
                                };
                         ],

          //Final callback
          function(err, results) {
          connection.end();
                                 };
                       );
     })(Myurl,ThisName);
   };

EDIT :

I got an error with the suggested answer TypeError: object is not a function , at the line return next() in the if statement . In my real code I actually do something like that (when I use the suggested answer):

async.series([
  //First function
  function(callback){
    //Some calculations...
    callback();
 },
  //Next function
  function(callback){
       var i = 0;
       //Loop
       async.whilst(
       function() { return i <= thefooz.length-1; },
       //The innerCallback:
       function(innerCallback){
       //Some calculations where i get urls and TheNames.
async.forEachOf(urls, function(Myurl, m, eachDone) {
  var ThisName = TheNames[m];

  async.waterfall([
    function(next) {
      connection.query(
        'SELECT * FROM mydata WHERE UrlLink=? LIMIT 1',
        [ Myurl ],
        next
      );
    },
    function(results, next) {
      if (results.length !== 0) {
        return next();
      }
      console.log("New Thing!");
      //Do some stuff here, request(Myurl...) to find TableName.
      var post = {
        UniqueUrl : Myurl,
        ThingName : ThisName,
        TheName   : TableName
      };
      connection.query('Insert INTO mydata Set ?', post, next);
    }
  ], eachDone);

}, function(err) {
  if (err) throw err; // or however you like to handle errors
  connection.end();
});
setTimeout(function() { i++; innerCallback(); }, 10000);
}); //close my calculations in innerCallback.
     }, //close innerCallback.

    ); //close asyns.whilst
callback;
   } //close function(callback)

      ], function(error){
        if (error) return next(error);

      });

I am really stuck in callback hell ...

An (untested) version that should do the same, but with some bugfixes and 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 all url's in parallel (at the same time). If you to process them
// sequentially (one after another), you can use `async.forEachOfSeries()`
// instead. Or, if you want to limit the 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 * FROM mydata WHERE UrlLink=? LIMIT 1',
        [ Myurl ],
        next
      );
    },
    function(results, fields, next) {
      if (results.length !== 0) {
        return next();
      }
      console.log("New Thing!");
      //Do some stuff here, request(Myurl...) to find TableName.
      var post = {
        UniqueUrl : Myurl,
        ThingName : ThisName,
        TheName   : TableName
      };
      connection.query('Insert INTO mydata Set ?', post, next);
    }
  ], eachDone);

}, function(err) {
  if (err) throw err; // or however you like to handle errors
  connection.end();
});

Instead of using forEach* , you can also use each* , but since you need the array index as well forEach* is more convenient.

Relevant documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM