i have this function for return array of string .
async GetAllPermissonsByRoles(id) {
let model: string[] = [];
try {
id.forEach(async (role) => {
let permission = await RolePermissionModel.find({ roleId: role._id })
.populate({
path: 'permissionId',
model: 'Permission'
});
permission.forEach((elment) => {
model.push(elment.permissionId);
});
})
} catch (error) { }
return model;
}
but i have a problem with this code , in this function not wait for complete forLoop
and then return model
, loop is not complete yet but returns the value .
whats the problem ? how can i sovle this problem ?
Update :
i write this code but still have that problem :
async GetAllPermissonsByRoles(ids) {
let model: string[] = [];
try {
await Promise.all(ids.map(async (role) => {
let permission = await RolePermissionModel.find({ roleId: role._id })
.populate({
path: 'permissionId',
model: 'Permission'
});
await Promise.all(permission.map(async (permission) => {
permission.forEach((elment) => {
model.push(elment.permissionId);
});
}))
}))
} catch (error) { }
return model;
}