javascript - Can't get property of objects I've created. Looks like problems with mongoose for MongoBD -


upd. problem solved 1 line of code: .lean() axplanation here

i have array of menu items after 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 want build tree menu them:

function buildmenu(source){         var menu = []; //Массив для нового, уже пирамидального меню             (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; } 

so, when trying console.log menu[0].items - have them:

[ { _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' } ]

but then, when trying return new amazing menu, this:

[ { _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 } ]

my god! items dissapeared!?!

upd. expecting this:

[ { _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' }       ]              ..... 

here how data:

 .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));                 }             });         //     })  

menus:

enter image description here

users:

enter image description here

groupes:

enter image description here

i realy confused...

enter image description here

postman cant see them also:

enter image description here

you build tree object , creating tree random source order.

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);


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -