[英]Nested array with knex and node.js
I currently have a table in the database that I use to register the system menus, it is called menu and has a field id_menu, dsc_menu and parent_item.我目前在数据库中有一个用于注册系统菜单的表,它被称为菜单,并且有一个字段 id_menu、dsc_menu 和 parent_item。 Id_menu is autoincrement and parent_item receives the id_menu of its main menu items.
Id_menu 是自动增量的,而 parent_item 接收其主菜单项的 id_menu。 In this case, they become submenus.
在这种情况下,它们成为子菜单。 It turns out that I have tried in many ways to generate a json by nesting with my query, that is, the main menu items and if there is one, a node containing the submenus of that item in question.
事实证明,我已经尝试以多种方式通过嵌套我的查询来生成 json,即主菜单项,如果有一个,则包含一个包含相关项的子菜单的节点。 I thought about making a first query, which will bring me only the main menus, then I would do a foreach in this array, take the id_menu and make a new query now only for the items that have parent_item = id_menu.
我考虑进行第一个查询,它只会给我带来主菜单,然后我会在这个数组中做一个 foreach,获取 id_menu 并现在只针对具有 parent_item = id_menu 的项目进行新查询。 Then I could concatenate this data again and transform it into a complete array.
然后我可以再次连接这些数据并将其转换为一个完整的数组。 I made an outline below my idea, however, it doesn't work.
我在我的想法下面做了一个大纲,但是,它不起作用。
// here I take all the parent menus
var result = await knex.raw("SELECT * FROM menus WHERE parent_item is null");
result[0].forEach(element => {
// Here I need to use the parent menu id to get the child menus and add them to a new array that will be inside the "submenus" element of the main array.
if(element.num_sub_menus > 0) {
var jsonSubMenus = await knex.raw("SELECT dsc_menu FROM menus WHERE parent_item = ?", element.num_sub_menus);
// if I try to call the knex again here, using await, it gives an error and if I don't use awaita it returns an empty promisse.
}
resultado += '{';
resultado += '"id_menu_admin": ' + element.id_menu_admin + ',';
resultado += '"dsc_menu": "' + element.dsc_menu + '",';
resultado += '"parent_item": ' + element.parent_item + ',';
resultado += '"rota": "' + element.rota + '",';
resultado += '"icone": "' + element.icone + '",';
resultado += '"num_sub_menus": ' + element.num_sub_menus + ',';
resultado += '"submenus": ['+jsonSubMenus+']';
resultado += '},';}
);
For subqueries, either return an empty promise if I don't use await, as an example, or give a compilation error if I use await.对于子查询,例如,如果我不使用 await,则返回一个空的 promise,或者如果我使用 await,则给出编译错误。
I imagine that there must be a more professional way of doing this, bringing a nested array through a single query.我想必须有一种更专业的方式来做到这一点,通过单个查询带来一个嵌套数组。 Remembering that my menu items and submenu are in the same table.
记住我的菜单项和子菜单在同一个表中。
While you could use a for
loop instead of forEach
so that await
would be permitted inside, it'd be better to use a somewhat different approach - use Promise.all
so that all submenus are queried at once, in parallel, then construct the resultado
.虽然您可以使用
for
循环而不是forEach
以便在内部允许await
,但最好使用稍微不同的方法 - 使用Promise.all
以便一次并行查询所有子菜单,然后构造resultado
.
It looks like resultado
is JSON, so you should use an object literal and then JSON.stringify
instead of manually concatenating.看起来
resultado
是 JSON,因此您应该使用 object 文字,然后使用JSON.stringify
而不是手动连接。 Something along the lines of:类似于以下内容:
const getSubmenus = elm => elm.num_sub_menus > 0
? knex.raw("SELECT dsc_menu FROM menus WHERE parent_item = ?", elm.num_sub_menus)
: null;
const withSubmenus = await Promise.all(result[0].map(
elm => Promise.all([element, getSubmenus(element)])
);
const resultArr = withSubmenus.map(([e, submenus]) => ({
id_menu_admin: e.id_menu_admin,
dsc_menu: e.dsc_menu,
// ...,
...(submenus ? { submenus } : {})
}));
const resultJSON = JSON.stringify(resultArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.