gpt4 book ai didi

javascript - 无法获取我创建的对象的属性。看起来 MongoDB 的 mongoose 有问题

转载 作者:行者123 更新时间:2023-11-27 22:45:12 29 4
gpt4 key购买 nike

UPD。一行代码解决了问题:.lean() axplanation here

我在 Model.find(...blablabla :

[ {"_id":"578763de6e8e0542195ef4e8", "text":"lists", "iconCls":"fa fa-group fa-lg", "className":null, "menu_id":null},

{"_id":"578762146e8e0542195ef4e7", "iconCls":"fa fa-group fa-lg","className":"panel", "menu_id":"578763de6e8e0542195ef4e8", "text":"personal"},

{"_id":"578770aca59f4d173c948376", "text":"info", "iconCls":"xf007", "className":null, "menu_id":null},

{"_id":"5787715aa59f4d173c94837c", "text":"cars", "className":"panel", "menu_id":"578763de6e8e0542195ef4e8", "iconCls":"xf007"},

{"_id":"5787721ca59f4d173c94837f", "text":"now" ,"iconCls":"xf007", "className":"xf007", "menu_id":"578770aca59f4d173c948376"}]

And I want to build tree menu from them:

function buildMenu(source){    
var menu = []; //Массив для нового, уже пирамидального меню
for (var i=0; i<source.length; i+=1){
if(source[i].menu_id === null){ //Выбираем верхние пункты меню
source[i].items = [];
for(var j=0; j<source.length;j+=1){
if(source[j].menu_id !== null){
if(source[i]._id.toString() == source[j].menu_id.toString()){
//найден дочерний пункт меню
source[i].items.push(source[j]);
}
}
}
menu.push(source[i]);
}
}
// Проверка
console.log(menu[0].items);
console.log(menu);
return menu;
}

所以,当我尝试 console.log 我的菜单[0].items - 我有它们:

[ { _id: 578762146e8e0542195ef4e7,    iconCls: 'fa fa-group fa-lg',    className: 'panel',    menu_id: 578763de6e8e0542195ef4e8,    text: 'personal' },  { _id: 5787715aa59f4d173c94837c,    text: 'cars',    className: 'panel',    menu_id: 578763de6e8e0542195ef4e8,    iconCls: 'xf007' } ]

但是,当我试图返回我的新的令人惊叹的菜单时,我得到了这个:

[ { _id: 578763de6e8e0542195ef4e8,    text: 'lists',    iconCls: 'fa fa-group fa-lg',    className: null,    menu_id: null },  { _id: 578770aca59f4d173c948376,    text: 'info',    iconCls: 'xf007',    className: null,    menu_id: null } ]

天啊!我的元素在哪里消失了!?!

更新。我期待得到这样的东西:

[ { _id: 578763de6e8e0542195ef4e8,    text: 'lists',    iconCls: 'fa fa-group fa-lg',    className: null,    menu_id: null     items: [          { _id: 578762146e8e0542195ef4e7,         iconCls: 'fa fa-group fa-lg',         className: 'panel',         menu_id: 578763de6e8e0542195ef4e8,         text: 'personal' },         { _id: 5787715aa59f4d173c94837c,         text: 'cars',         className: 'panel',         menu_id: 578763de6e8e0542195ef4e8,         iconCls: 'xf007' }      ]             .....

以下是我获取数据的方式:

.get('/menu', function(req,res){                var user_id = '5784b872a59f4d0f805a617d'; //пользователь boss/boss        User.find({_id:user_id})            .populate({                path: 'group',                populate: {                    path: 'menu',                    ref: 'Menu'                }            })            .exec(function(err,user) {                if (err) {                    console.log(err.red, req.method, req.url);                    res.status(500).end();                    return err;                } else if (!user) {                    res.status(404).end();                    console.log(('Запрос вернул: ' + user).red);                } else {                    res.status(200).send(buildMenu(user[0].group.menu));                }            });        //    }) 

菜单:

enter image description here

用户:

enter image description here

团体:

enter image description here

我真的很困惑......

enter image description here

postman 也看不到它们:

enter image description here

最佳答案

您可以使用一个对象来构建树,用于查找并使用随机源顺序创建树。

var source = [{ "_id": "578763de6e8e0542195ef4e8", "text": "lists", "iconCls": "fa fa-group fa-lg", "className": null, "menu_id": null }, { "_id": "578762146e8e0542195ef4e7", "iconCls": "fa fa-group fa-lg", "className": "panel", "menu_id": "578763de6e8e0542195ef4e8", "text": "personal" }, { "_id": "578770aca59f4d173c948376", "text": "info", "iconCls": "xf007", "className": null, "menu_id": null }, { "_id": "5787715aa59f4d173c94837c", "text": "cars", "className": "panel", "menu_id": "578763de6e8e0542195ef4e8", "iconCls": "xf007" }, { "_id": "5787721ca59f4d173c94837f", "text": "now", "iconCls": "xf007", "className": "xf007", "menu_id": "578770aca59f4d173c948376" }],
tree = function (data, root) {
var r = [], o = {};
data.forEach(function (a) {
a.items = o[a._id] && o[a._id].items;
o[a._id] = a;
if (a.menu_id === root) {
r.push(a);
} else {
o[a.menu_id] = o[a.menu_id] || {};
o[a.menu_id].items = o[a.menu_id].items || [];
o[a.menu_id].items.push(a);
}
});
return r;
}(source, null);

console.log(tree);

关于javascript - 无法获取我创建的对象的属性。看起来 MongoDB 的 mongoose 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38457961/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com