mysql - 如何在 Node 中链接彼此依赖的sql查询

标签 mysql arrays node.js async.js

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

我所尝试的方法都没有给我想要的结果,因为数组循环在 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
  }}
});

最佳答案

可能我来晚了一点,但我认为 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;
});  

关于mysql - 如何在 Node 中链接彼此依赖的sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31891943/

相关文章:

php - MySQL Select 查询 - 限制不是记录数,而是返回结果的类型

mysql - 第 2 行存在语法错误 (1064)

php - Opencart 1.5 结帐确认支付网关花费太多时间

mysql - 找出两个记录之间的时间

c - 如何扫描二维数组?

c++ - 如何在 C++ 中将二维数组转换为单个字符串

javascript - console.log 和 valueOf

php - 另一个php数组循环问题

javascript - 将定义添加到 Typescript 中的现有模块

node.js - 比较服务器上的客户端时间