NodeJS中的异步MySQL循环

时间:2014-12-28 18:59:53

标签: node.js asynchronous

上周,我一直在使用NodeJS开发一款新应用。我现在有一个工作区准备好开始编写核心。

我已经习惯了PHP,已经使用它3年了,即使它提供了异步功能,我也从来没有和它们一起工作过,所以我需要一些帮助。

我有一个名为PRODUCTS的表和另一个名为PRODUCT_IMAGES的表,我需要获取现有产品,并且每个产品都从第二个表中提取图像。

代码:

user.findProducts = function( request, response ){
        var sql = "SELECT `P`.`ID`,`P`.`NAME`,`P`.`DESCRIPTION`,`P`.`PRICE`,`P`.`HARVESTED_IN`,`L`.`NAME` AS `LOCAL_NAME`,`L`.`LOCAL_PHONE` FROM `PRODUCT` AS `P` LEFT OUTER JOIN `LOCAL` AS `L` ON `P`.`ID_LOCAL` = `L`.`ID` WHERE `P`.`ACTIVE` = ?";
        app.execQuery({
            sql : sql,
            params : [1],
            onSuccess : function( SQLRowsResult ){
                var row = null;
                var products = [];
                app.pImages = [];
                for ( var i = 0; i < SQLRowsResult.length; i++ ) {
                    row = SQLRowsResult[i];
                    var sql = "SELECT `ID`, `URL` FROM `PRODUCT_IMAGE` WHERE `ID_PRODUCT` = ?";
                    app.execQuery({
                        sql : sql,
                        params : [row.ID],
                        onSuccess : function( SQLRowsResult2 ){
                            var row2 = null;
                            for ( var j = 0; j < SQLRowsResult2.length; j++ ) {
                                row2 = SQLRowsResult2[j];
                                var Img = {
                                    ID: row2.ID,
                                    URL: row2.URL
                                }
                                console.log(1);
                                app.pImages.push( Img );
                            }
                            return app.pImages;
                        }
                    });
                    var Product = {
                        ID: row.ID,
                        Name: row.NAME,
                        Description: row.DESCRIPTION,
                        Price: row.PRICE,
                        HarvestedAt: row.HARVESTED_IN,
                        LocalName: row.LOCAL_NAME,
                        LocalPhone: row.LOCAL_PHONE,
                        Images : app.pImages
                    }
                    console.log( 2 );
                    products.push( Product );
                };
                console.log( products );
                response.send( products );
            }
        });
    }

输出:

2
2
[ { ID: 3,
    Name: 'BANANO',
    Description: 'Este banano es muy rico, tiene un sabor muy dulce y vienes de las tierras de limón donde la vida no vale nada pero tienen playa so they got that going on 4 them which is nice. En fin, compren de mi banano y comanlo principalmente que les hace bueno para l',
    Price: 5,
    HarvestedAt: 'TACARES',
    LocalName: 'Frutas La Esquina de José',
    LocalPhone: '24941715',
    Images: [] },
  { ID: 4,
    Name: 'KIWI',
    Description: 'LAS TETAS',
    Price: 100,
    HarvestedAt: 'ZARCERO',
    LocalName: 'PRISC',
    LocalPhone: '59+85645',
    Images: [] } ]
1
1

据我所知,异步功能是什么创建这种执行,这是我无法弄清楚的是如何使回调函数将图像推送到产品对象。

1 个答案:

答案 0 :(得分:0)

async模块可以帮助管理更复杂的异步操作。例如:

async.each(SQLRowsResult, function(row, callback) {
  var Product = {
        ID: row.ID,
        Name: row.NAME,
        Description: row.DESCRIPTION,
        Price: row.PRICE,
        HarvestedAt: row.HARVESTED_IN,
        LocalName: row.LOCAL_NAME,
        LocalPhone: row.LOCAL_PHONE,
        Images : app.pImages
      },
      sql = "SELECT `ID`, `URL` FROM `PRODUCT_IMAGE` WHERE `ID_PRODUCT` = ?";

  app.execQuery({
    sql : sql,
    params : [row.ID],
    onSuccess : function( SQLRowsResult2 ){
      var row2;
      for ( var j = 0; j < SQLRowsResult2.length; j++ ) {
        row2 = SQLRowsResult2[j];
        var Img = {
          ID: row2.ID,
          URL: row2.URL
        }
        app.pImages.push( Img );
      }
      products.push( Product );
      callback();
    }
  });
}, function(err) {
  // all done
});