查询后使用nodejs进行查询并续作

时间:2015-12-25 16:56:10

标签: mysql node.js asynchronous subquery sequelize.js

我想在node.js中使用sequqlize在另一个查询之后进行查询。第二个查询迭代第一个元素查询结果。

代码:

exports.index = function(req, res) {
  sqldb.sequelize.query("select * from food", {type: sqldb.sequelize.QueryTypes.SELECT})
    .then(function (food) {
        for (var i = food.length; i--;) {
          sqldb.sequelize.query("select     i.name_ingredient " +
            "from   food_incredients as fi, " +
            "ingredients as i " +
            "where  fi.food_id = " + food[i].id + " and " +
            "i.id = fi.ingredient_id;",
            {type: sqldb.sequelize.QueryTypes.SELECT}).then(function (ingredients) {
            food[i]["ingredients"] = ingredients;
          });
        }

        res.status(200).json(food);
      }
    );

当我尝试添加" infredients"我发现了一个错误,即食物[i]"对象未定义。我怎样才能解决这个问题并返回"食物"每种元素的成分?

由于

0 个答案:

没有答案