Node.js JSON响应中数组内容的顺序不一致

Node.js JSON响应中数组内容的顺序不一致,node.js,express,es6-promise,knex.js,Node.js,Express,Es6 Promise,Knex.js,所以,我在Postgres中有一个嵌套的集合表。在我的Express应用程序中,我有以下控制器功能代码,使用Knex和一些promise处理将嵌套类别作为数组: const getCategories = (req, res, db) => { const products = [] db.raw(` SELECT child.id, child.name, child.path FROM product_category parent JOIN prod

所以,我在Postgres中有一个嵌套的集合表。在我的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"
      }
    ]
  }
]
或者像这样:

[
  {
    "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"
      }
    ]
  }
]
如何使其一致?也许这有点琐碎,但过了一会儿就有点烦人了。提前谢谢

在subreddit中,以增加它得到回答的机会。用户给出了一个答案

基本上,这是因为我在代码段上写了错误的代码。我将引用他的全部答案:

我认为这是因为您正在从内部构建产品阵列 嵌套查询的承诺链。根据查询所需的时间, 产品最终会有不同的订单

在处理承诺时,这通常被认为是不好的。信息技术 基本上打破了由您的 使用地图和承诺

您应该更改:

products.push(categoryObject)
…致:

return categoryObject
return Promise.all(categoryPromises)
  .then(products => res.json(products))
和变化:

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

return categoryObject
return Promise.all(categoryPromises)
  .then(products => res.json(products))
并删除:

const products = []
编辑:为了澄清,他没有StackOverflow帐户,他允许我发布他的答案。

在subreddit中增加获得答案的机会。用户给出了一个答案

基本上,这是因为我在代码段上写了错误的代码。我将引用他的全部答案:

我认为这是因为您正在从内部构建产品阵列 嵌套查询的承诺链。根据查询所需的时间, 产品最终会有不同的订单

在处理承诺时,这通常被认为是不好的。信息技术 基本上打破了由您的 使用地图和承诺

您应该更改:

products.push(categoryObject)
…致:

return categoryObject
return Promise.all(categoryPromises)
  .then(products => res.json(products))
和变化:

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

return categoryObject
return Promise.all(categoryPromises)
  .then(products => res.json(products))
并删除:

const products = []
编辑:为了澄清,他没有StackOverflow帐户,他允许我发布他的答案