For example I have one table:
Menuid, menuname, parentid, levelid -- this is table
Consider the values:
Menuid:Pg, menuname:mainprogram, programid:'', levelid: 1
Menuid:sb, menuname:subprogram, programid:'Pg', levelid: 2
Menuid:cb, menuname:childprogram, programid:'sb', levelid: 3
Menuid:Pg2, menuname:mainprogram2, programid:'', levelid: 1
Output:
[ {
Menuid:'Pg',
Menuname:'mainprogram',
Children:[ {
Menuid:'sb',
Menuname:'subprogram',
Children:[ {
Menuid:'cb',
Menuname:'childprogram'
} ]
} ]
}, {
Menuid:'Pg2',
Menuname:'mainprogram2'
} ]
getting menu code
exports.getMenus = (req, res, next) => {
// need clarification
}
model for menu
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const menuSchema = mongoose.Schema({
menuId: {
type: String,
required: true,
unique: true
},
menuName: {
type: String,
required: true
},
icon: {
type: String,
required: false
},
type: {
type: String,
required: false
},
url: {
type: String,
required: false
},
levelId: {
type: Number,
required: true
},
programId: {
type: String,
required: false
},
order: {
type: Number,
required: true
},
createdBy: {
type: String,
require: false
},
createdDate: {
type: Date,
require: false,
},
updatedBy: {
type: String,
default: '',
require: false
},
updatedDate: {
type: Date,
default: '',
require: false,
},
});
menuSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Menu', menuSchema);
router:
router.get('/getmenus/:id', MenuController.getMenus);
user request from client like http://localhost:3000/api/menu/getmenus/pg getmenus after slash word is a required menus under submenus
Note: if each data has order menans based on menu object need to render with that order. Please provide any solution.