数组内容的顺序在JSON响应中不一致

时间:2019-03-10 06:44:51

标签: node.js express es6-promise knex.js

因此,我在here's the schema and data的Postgres中有此嵌套的set表。我在Express应用程序中使用了以下控制器功能代码,以使用Knex和一些promise处理将嵌套类别作为数组获取:

const getCategories = (req, res, db) => {
  const products = []

  db.raw(`
    SELECT child.id, child.name, child.path
    FROM product_category parent
    JOIN product_category child
    ON child.lower BETWEEN parent.lower AND parent.upper
    WHERE parent.id = 1
      AND
        (
          SELECT COUNT(*)
          FROM product_category node
          WHERE child.lower BETWEEN node.lower AND node.upper
            AND node.lower BETWEEN parent.lower AND parent.upper
        ) = 2 
  `)
  .then(categories => {
    if (categories.rows.length) {
      const categoryPromises = categories.rows.map(category => {
        return db.raw(`
          SELECT child.id, child.name, child.path
          FROM product_category parent
          JOIN product_category child
          ON child.lower BETWEEN parent.lower AND parent.upper
          WHERE parent.id = ${category.id}
          AND
            (
              SELECT COUNT(*)
              FROM product_category node
              WHERE child.lower BETWEEN node.lower AND node.upper
                AND node.lower BETWEEN parent.lower AND parent.upper
            ) = 2 
        `)
        .then(subcategories => {
          const categoryObject = { ...category, subcategories: [] }
          if (subcategories.rows.length) {
            subcategories.rows.map(subcategory => {
              categoryObject.subcategories.push(subcategory)
            })
            products.push(categoryObject)
          } else {
            res.status(400).json("No subcategories")
          }
        })
      })

      return Promise.all(categoryPromises)
      .then(() => res.json(products))

    } else {
      res.status(400).json("No categories")
    }
  })
}

获取响应没有问题,但是数组中对象的第一级的顺序不一致。有时是这样的:

[
  {
    "id": 9,
    "name": "Other Products",
    "path": "other_products",
    "subcategories": [
      {
        "id": 10,
        "name": "Slides",
        "path": "slides"
      },
      {
        "id": 11,
        "name": "Buoys",
        "path": "buoys"
      }
    ]
  },
  {
    "id": 2,
    "name": "Boats",
    "path": "boats",
    "subcategories": [
      {
        "id": 3,
        "name": "Rescue Boats",
        "path": "rescue_boats"
      },
      {
        "id": 4,
        "name": "Dive Boats",
        "path": "dive_boats"
      },
      {
        "id": 5,
        "name": "Tamarans",
        "path": "tamarans"
      },
      {
        "id": 6,
        "name": "Dragon Boats",
        "path": "dragon_boats"
      },
      {
        "id": 7,
        "name": "Kayaks",
        "path": "kayaks"
      },
      {
        "id": 8,
        "name": "Speedboats",
        "path": "speedboats"
      }
    ]
  }
]

或者这样:

[
  {
    "id": 2,
    "name": "Boats",
    "path": "boats",
    "subcategories": [
      {
        "id": 3,
        "name": "Rescue Boats",
        "path": "rescue_boats"
      },
      {
        "id": 4,
        "name": "Dive Boats",
        "path": "dive_boats"
      },
      {
        "id": 5,
        "name": "Tamarans",
        "path": "tamarans"
      },
      {
        "id": 6,
        "name": "Dragon Boats",
        "path": "dragon_boats"
      },
      {
        "id": 7,
        "name": "Kayaks",
        "path": "kayaks"
      },
      {
        "id": 8,
        "name": "Speedboats",
        "path": "speedboats"
      }
    ]
  },
  {
    "id": 9,
    "name": "Other Products",
    "path": "other_products",
    "subcategories": [
      {
        "id": 10,
        "name": "Slides",
        "path": "slides"
      },
      {
        "id": 11,
        "name": "Buoys",
        "path": "buoys"
      }
    ]
  }
]

我如何使其一致?也许这有点琐碎,但过了一会儿却变得有些烦人。预先感谢!

1 个答案:

答案 0 :(得分:0)

I also asked中的

r/learnprogramming subreddit增加了被回答的机会。用户u/shhh-quiet给出了nice solution

基本上,这是因为我在代码段中编写了错误的代码。我只引用他的完整答案:

  

我认为这是您从内部构建产品阵列   嵌套查询的诺言链。根据查询需要多长时间,   产品最终将具有不同的订购。

     

在履行诺言时,这通常被认为是不好的。它   基本上摆脱了您否则建立的排序   使用map和Promise.all。

     

您应该更改:

products.push(categoryObject)
     

...至:

return categoryObject
     

然后更改:

return Promise.all(categoryPromises)
  .then(() => res.json(products))
     

...至:

return Promise.all(categoryPromises)
  .then(products => res.json(products))
     

然后删除:

const products = []

编辑:为清楚起见,他没有StackOverflow帐户,并且他允许我发布答案。