所以,我在Postgres中有这个嵌套的表格,here's the schema and data。我在我的Express应用程序中有下面的控制器功能代码,使用Knex和一些承诺处理将嵌套类别作为数组获取:
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"
}
]
}
]
如何使其保持一致?也许它有点微不足道,但一段时间后它会变得有点烦人。提前致谢!
在I also asked的r/learnprogramming subreddit,以增加它得到答案的机会。用户u/shhh-quiet给了nice solution。
基本上,这是因为我在我的代码片段上编写了错误的代码。我只想引用他的全部答案:
我认为你是从嵌套查询的承诺链中构建产品数组。根据查询的时间长短,产品最终会有不同的排序。
在使用promises时,这通常被认为是错误的。它基本上摆脱了你使用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帐户,他允许我发布他的答案。