【问题标题】:How to chain sql queries which are dependent on each other in node如何链接节点中相互依赖的sql查询
【发布时间】:2015-10-31 17:41:57
【问题描述】:

我希望从查询中获取数据,然后对存储在结果中的项目运行数组循环。然后使用从第一个查询中获取的数组中的键从另一个查询中获取数据。

我所尝试的并没有给我想要的结果,因为数组循环在 sql 查询完成并产生所需的结果之前用完。

我已尝试使用 async.series 函数和 async.map 函数的解决方案,但结果未按所需顺序出现。

这是第一个查询的输出。 我的目标是在从第一个查询返回的选项 ID 中添加选项详细信息数组。

[
  {
    "id": 318,
    "name": "Sandwich Dosa",
    "price": 140,
    "option": ''
  },
  {
    "id": 319,
    "name": "Spi. Prem Uttappa",
    "price": 131,
    "option": '1,2'
  },
  {
    "id": 320,
    "name": "Paneer Spl. Prem Uttappa",
    "price": 140,
    "option": ''
  },
  {
    "id": 321,
    "name": "Spl. Spicy Uttappa",
    "price": 131,
    "option": ''
  }
]

希望构建为

[
  {
    "id": 318,
    "name": "Sandwich Dosa",
    "price": 140,
    "option": ''
  },
  {
    "id": 319,
    "name": "Spi. Prem Uttappa",
    "price": 131,
    "option": [{
      id:'1',
      name:'Regular',
      price:'89',
    },
    {
      id:'2',
      name:'Spicy',
      price:'119',
    }]
  },
  {
    "id": 320,
    "name": "Paneer Spl. Prem Uttappa",
    "price": 140,
    "option": ''
  },
  {
    "id": 321,
    "name": "Spl. Spicy Uttappa",
    "price": 131,
    "option": ''
  }
]

这是我目前正在尝试实现的代码

var data = {
  "error": 1,
  "items": ""
};

connection.query("SELECT * FROM  items WHERE status=1 AND  menu_grp_id=" + req.params.sid, function(err, rows, fields) {
  var items = []; // temporary holder for items

  if(err) { throw err } else {
  if(rows.length != 0) {
    for (var key in rows) {
      // Holding Item properties
      var item = {
        id: '',
        name: '',
        price: '',
        option: ''
      };
      item.id = rows[key].id;
      item.name = rows[key].item_name;
      item.price = rows[key].price;
      item.option = rows[key].option;

      items.push(item); // pushing item object into    items array
    }
    data["error"] = 0;      
    data['items'] = items;
    if((data['items'].length > 0)) {
      for(i = 0; i < data['items'].length; i++) {
        if(data['items'][i].option == "") {
          data['items'][i].option = [];
        } else {
          var array = data['items'][i].option.split(",")
          data['items'][i].option = array;
        }
      }
    }
    res.json(data); // sending JSON Data
  } else {
    data["error"] = 0;
    data["items"] = 'No item Found..';
    res.json(data); //  sending JSON Data
  }}
});

【问题讨论】:

    标签: mysql arrays node.js async.js


    【解决方案1】:

    我在这里可能有点晚了,但我认为 async.waterfall 是您所需要的。

    async.waterfall([
        function(cb){
            connection.query("SELECT * FROM  items WHERE status=1 AND  menu_grp_id=" + req.params.sid, function(err, rows, fields) {
                //manipulate your data as you wish
                data = synchronousManipolutionOfYourData();
                cb(null, data);
            });
        },
        function(data, cb){
            //data here stores the data you passed as a second parameter in the preciding callback
           //execute your second query with the data you got from the first query.
           cb(null, dataManipulatedAfterSecondQuery)//and so on
        }, 
        //other functions if you need other queries
    ], function(){
        done;
    });  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多