递归函数在express js中没有返回值



我在一家电子商务网站工作。我已经为所有类别创建了一个猫鼬模型。但包括作为子类别的类别的父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"递归函数不起作用。

请任何人帮帮我,我是这个领域的新手。。。

由于_id属性是ObjectId的实例,因此它们永远不会与作为字符串的parentId匹配。

根据文件:

ObjectId()具有以下属性和方法:

属性/方法 描述
str 返回对象的十六进制字符串表示形式

更改下面的代码

children: createCategory(categories, cate._id)

收件人:

children: createCategory(categories, cate._id.toString())

最新更新