【发布时间】:2021-11-07 10:51:29
【问题描述】:
我在电子商务网站工作。我已经为所有类别创建了一个猫鼬模型。但包括作为子类别的类别的父 ID。 当收到请求发送时,所有类别都是从数据库中找到的。很好,但我想以嵌套的方式将类别发送给客户端。这就是我这样写的原因。
// This is mongoose model
const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
unique: true
},
slug: {
type: String,
required: true,
unique: true
},
parentId: {
type: mongoose.Schema.Types.ObjectId
}
}, { timestamps: true })
module.exports = mongoose.model("Category", categorySchema)
// This is router
Router.get('/categories', async (req, res) => {
const createCategory = (categories, parentId = null) => {
const categoryList = []
let category
if (parentId === null) {
category = categories.filter(cat => cat.parentId === undefined);
} else {
category = categories.filter(cat => cat.parentId === parentId)
}
category.map((cate) => {
categoryList.push({
_id: cate._id,
name: cate.name,
slug: cate.slug,
children: createCategory(categories, cate._id)
})
})
return categoryList
}
try {
const categories = await Category.find({})
const categoryList = createCategory(categories)
res.status(200).json({ categoryList })
} catch (error) {
res.status(500).json(error)
}
})
// the data of databess
[
{
_id: new ObjectId("613c4ae2ff0098e41b1ae89a"),
name: 'Mobile',
slug: 'Mobile',
createdAt: 2021-09-11T06:21:22.137Z,
updatedAt: 2021-09-11T06:21:22.137Z,
__v: 0
},
{
_id: new ObjectId("613c4b11ff0098e41b1ae89c"),
name: 'Oppo',
slug: 'Oppo',
parentId: '613c4ae2ff0098e41b1ae89a',
createdAt: 2021-09-11T06:22:09.068Z,
updatedAt: 2021-09-11T06:22:09.068Z,
__v: 0
},
{
_id: new ObjectId("613c4b21ff0098e41b1ae89e"),
name: 'Samsung',
slug: 'Samsung',
parentId: '613c4ae2ff0098e41b1ae89a',
createdAt: 2021-09-11T06:22:25.359Z,
updatedAt: 2021-09-11T06:22:25.359Z,
__v: 0
},
{
_id: new ObjectId("613c4b2fff0098e41b1ae8a0"),
name: 'Nokia',
slug: 'Nokia',
parentId: '613c4ae2ff0098e41b1ae89a',
createdAt: 2021-09-11T06:22:39.048Z,
updatedAt: 2021-09-11T06:22:39.048Z,
__v: 0
},
{
_id: new ObjectId("613c4b47ff0098e41b1ae8a2"),
name: 'Nokia 2.4',
slug: 'Nokia-2.4',
parentId: '613c4ae2ff0098e41b1ae89a',
createdAt: 2021-09-11T06:23:03.580Z,
updatedAt: 2021-09-11T06:23:03.580Z,
__v: 0
},
{
_id: new ObjectId("613c4b6cff0098e41b1ae8a4"),
name: 'TV',
slug: 'TV',
createdAt: 2021-09-11T06:23:40.550Z,
updatedAt: 2021-09-11T06:23:40.550Z,
__v: 0
},
{
_id: new ObjectId("613c4b7eff0098e41b1ae8a6"),
name: 'Refrijerator',
slug: 'Refrijerator',
createdAt: 2021-09-11T06:23:58.782Z,
updatedAt: 2021-09-11T06:23:58.782Z,
__v: 0
}
]
他们的问题是当我试图从客户端获取数据时,只有根级别的类别正在输出,它没有父 ID,但子数组是这样的空。
{
"categoryList": [
{
"_id": "613c4ae2ff0098e41b1ae89a",
"name": "Mobile",
"slug": "Mobile",
"children": []
},
{
"_id": "613c4b6cff0098e41b1ae8a4",
"name": "TV",
"slug": "TV",
"children": []
},
{
"_id": "613c4b7eff0098e41b1ae8a6",
"name": "Refrijerator",
"slug": "Refrijerator",
"children": []
}
]
}
我认为“createCategory”递归函数不起作用。
请任何人帮助我,我是这个领域的新手......
【问题讨论】:
-
如果你没有使用
.map()的结果,你应该使用.forEach() -
我无法重现此问题。当我尝试填充
children数组时。categories是什么?您能否编辑您的问题并为categories提供示例数据(JSON 格式),这会给您带来这种不希望的结果? -
如果你写
categoryList = category.map(cate => ...),你可以使用.map(),并让函数返回对象而不是推送它。 -
请输出
console.log(JSON.stringify(categories, null, 2))并将其包含在您的问题中。我想_id值是对象,而不是字符串(您似乎假设)。但请提供它,以便我们重现您的问题。
标签: javascript node.js express mongoose