我在一個電子商務網站作業。我已經為所有類別創建了一個mongoose模型。但包括作為子類的類別的父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"/span>, categorySchema)
//這就是路由器。
Router.get('/categories', async(req, res)=> {
const createCategory = (categories, parentId = null) => {
const categoryList = []
let 類別
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(錯誤)。
}
})
//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.
}
]
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
他們的問題是,當我試圖從客戶端獲取資料時,只有根級別的類別被輸出,它沒有父級ID,但子級陣列是空的,像這樣。
。{
"categoryList": [
{
"_id": "613c4ae2ff0098e41b1ae89a",
"name": "Mobile",
"slug": "Mobile",
"children": []
},
{
"_id": "613c4b6cff0098e41b1ae8a4",
"name": "TV",
"slug": "TV",
"children": []
},
{
"_id": "613c4b7eff0098e41b1ae8a6",
"name": "Refrijerator",
"slug": "Refrijerator",
"children": []
}
]
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我認為 "createCategory "的遞回功能不作業。
請任何人幫助我,我是這個領域的新手。
uj5u.com熱心網友回復:
由于_id屬性是ObjectId的實體,它們將永遠不會與parentId匹配,后者是字串。
根據檔案:
ObjectId()具有以下屬性和方法:
屬性/方法 描述str回傳物件的十六進制字串表示。
... ...
因此,改變這一行:
children: createCategory(categories, cate._id)
to:
children: createCategory(categories, cate._id.str)
備注
你對.map的使用是一種反模式。你應該取其回傳值,而不是在回呼中使用push。替換這兩條陳述句:
const categoryList = [] 。
/* ... */
category.map((cate) =>/span> {
categoryList.push({
_id: cate._id,
name: cate.name,
slug: cate.slug,
children: createCategory(categories, cate._id.str)
})
})
...用這個單一的陳述句:
const categoryList = category.map((cate) => /span> ({
_id: cate._id,
name: cate.name,
slug: cate.slug,
children: createCategory(categories, cate._id.str)
}))
其次,你可以避免在函式開始時的代碼重復,只要不給parentId引數提供一個默認值。因此,該函式可以變得更加緊湊:
const createCategory = (categories, parentId)=> {
const category = categories.filter(cat => cat.parentId == parentId) 。
return category.map((cate) =>/span> ({
_id: cate._id,
name: cate.name,
slug: cate.slug,
children: createCategory(categories, cate._id.str)
}));
};
uj5u.com熱心網友回復:
改變下面的代碼
。 children: createCategory(categories, cate._id)/code>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
致:
children。createCategory(categories, cate._id.toString())
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319851.html
標籤:
