提问人:Averroes 提问时间:10/8/2023 更新时间:10/8/2023 访问量:17
如何让 MongoDB 树状图中的子项显示为子项内的数组
How can I get children in a tree map from MongoDB to show up as an array inside a children key
问:
我正在尝试实现类似于 JSON 的输出,其中树状图中的子项显示在对象的“子项”键下。我问的原因是因为我正在使用 Primefaces (PrimeVue) 并且需要使用 TreeTable 组件来显示家谱。输出需要如下所示:
[
{
key: '0',
data: {
name: 'Applications',
size: '100kb',
type: 'Folder'
},
children: [
{
key: '0-0',
data: {
name: 'Vue',
size: '25kb',
type: 'Folder'
},
children: [
{
key: '0-0-0',
data: {
name: 'vue.app',
size: '10kb',
type: 'Application'
}
},
{
key: '0-0-1',
data: {
name: 'native.app',
size: '10kb',
type: 'Application'
}
},
{
key: '0-0-2',
data: {
name: 'mobile.app',
size: '5kb',
type: 'Application'
}
}
]
}
]
我目前的代码是这样的:
const fetchTrees = (source, root) => {
return new Promise((resolve, reject) => {
User.find({referrer: source}).then((children) => {
const promises = children.map((person, index) => {
const child = {
name: person.first_name + ' ' + person.last_name,
uppLine: person.uppLine,
_id: person._id
}
children.push(child)
root[index] = child
return fetchTrees(person._id, root[index])
})
Promise.all(promises).then((results) => {
if (results[0] !== null || results[0] !== undefined) {
resolve(root);
} else {
reject("No tree data");
}
})
.catch((error) => {
reject(error);
});
})
})
}
其输出为:
{
"0": {
"name": "Anthony Quinn",
"_id": "6515c57828f1601db4024deb"
},
"1": {
"name": "Peers Morgan",
"_id": "6515cc4280981f87ecd90598"
},
"2": {
"name": "Quick Silver",
"_id": "6515cd95bac94d41880dd091"
},
"3": {
"0": {
"0": {
"name": "Chris Mark",
"uppLine": "100",
"_id": "6520d88203d9eb1f9f0a092a"
},
"name": "Adam Curry",
"uppLine": "100",
"_id": "6518846650762f0c69a16bfb"
},
"name": "Larry David",
"_id": "651778cb1c60e355b88c3df6"
},
"4": {
"name": "Hugh Jackman",
"_id": "6515cf42a982c9a36ebc3505"
},
"5": {
"name": "Rihanna Rihanna",
"_id": "6515cfa73b15e4debcb13a1a"
}
}
正如你所看到的,输出只有对象表示法,没有输出子数组。
答: 暂无答案
评论