节点js +多个查询事务+附属API数据插入postgresql数据库

时间:2015-06-17 03:05:28

标签: node.js postgresql asynchronous

现在我正在使用Flipkart联盟API,并将类别和产品插入我的数据库。 我正在使用nodejs后端和postgresql数据库。

我使用了4个异步循环。但我做了一些事,但不知道正确的方法。在这里,我想在异步中执行循环,一旦所有进程(循环)结束意味着需要发送响应。如何确定成功执行的所有循环。请有人建议正确的方法。

var request = require('request');
var dateFormat = require('date-format');
var async = require('async');

  router.get('/getproducts', function(req, res) {
    var affiliateIdFrom = 1;
    var affiliateUrl = "https://affiliate-api.flipkart.net/affiliate/api/<your affiliate_tracking_id>.json";
    request(affiliateUrl, function(error, response, html) {
        var resData = JSON.parse(html);
        var dataObj = resData.apiGroups.affiliate.apiListings;

        async.each(Object.keys(dataObj), function(item, done) {
            var objData = dataObj[item].availableVariants['v0.1.0'];
            var cname = objData.resourceName;
            var curl = objData.get;
            client.query('INSERT into product_category (affiliate_from, name, url,created_on) values (' + "'" + affiliateIdFrom + "'" + ',' + "'" + cname + "'" + ',' + "'" + curl + "'" + ',' + "'" + today + "'" + ')', function(err, result) {
                if (err) {
                    console.log(err);
                    res.send({
                        data : "Soemthing went wrong",
                    });
                }
                done(err, result);
            });
        }, function(err) {

            product.getProductDetails(affiliateIdFrom);

        });

    });

    });
    var product = {
    getProductDetails : function(aId) {
      var today = dateFormat(new Date());

      client.query("select * from product_category where affiliate_from = " + aId + " limit 1", function(err, result) {
          if (err) {
              console.log(err);
          }
          var categoryList = result.rows;

          async.each(categoryList, function(value, done) {
              request.get({
                  headers : {
                      'Fk-Affiliate-Id' : 'XXXXX',
                      'Fk-Affiliate-Token' : 'XXXXXXXXXXXXXXXXXXXXXXX'
                  },                    
                  url : value.url
              }, function(error, response, body) {
                  var data = JSON.parse(body);
                  var dataLength = data.productInfoList.length;
                  var dataObj = data.productInfoList;

                  if (dataLength > 0) {
                      async.each(dataObj, function(value1, done1) {
                          var productBasicInfo = value1.productBaseInfo.productIdentifier;
                          var productId = productBasicInfo.productId;
                          var productDetails = value1.productBaseInfo.productAttributes;
                          var pTitle = productDetails.title;
                          var thumbnailUrl = JSON.stringify(productDetails.imageUrls);
                          var category = productBasicInfo.categoryPaths.categoryPath[0][0].title;
                          var mrp = productDetails.maximumRetailPrice.amount;
                          var price = productDetails.sellingPrice.amount;
                          var prodcutUrl = productDetails.productUrl;
                          var productDescription = productDetails.productDescription;
                          if (productDescription != null) {
                              productDescription = productDescription.replace(/'/g, "\''");
                          }
                          var inStock = productDetails.inStock;
                          var information = JSON.stringify(value1);
                          var q = 'insert into product(affiliate_from, title, image_url, category_paths, mrp, price, url, description, in_stock, information,  created_on) values (' + "'" + aId + "'" + ', ' + "'" + pTitle + "'" + ', ' + "'" + thumbnailUrl + "'" + ', ' + "'" + category + "'" + ', ' + "'" + mrp + "'" + ', ' + "'" + price + "'" + ', ' + "'" + prodcutUrl + "'" + ', ' + "'" + productDescription + "'" + ', ' + "'" + inStock + "'" + ', ' + "'" + information + "'" + ', ' + "'" + today + "'" + ' )';
                          console.log(q);
                          client.query(q, function(err, result) {
                              if (err) {
                                  console.log(err);
                              } else {
                                  console.log('inserted successfully');
                              }

                          });

                          done1("err", "result");

                      }, function(err) {
                          console.log('dddddddddd');
                      });
                  }
                  //console.log(data.productInfoList);
                  console.log(data.productInfoList.length);

                  done(err, "result");

              });

          }, function(err) {
              console.log('doneeeeee');

          });

      });


    }
    };

1 个答案:

答案 0 :(得分:0)

正确的方法是将每个异步请求放在一个事务中,一旦完成,它将指示整个查询集是否成功。

例如,使用pg-promise语法,您的交易就是这样:

db.tx(function () {
    return promise.all([
        this.query("insert 1 ..."),
        this.query("insert 2 ..."),
        this.query("insert 3 ...")
    ]);
})
    .then(function (data) {
        // success;
    }, function (reason) {
        // error
    });