I have a custom service which fetches data from multiple mongoose models and creates an array using these data and gives back to API.
Everything works well.. I can console the new object-array 'result.rolePermissions'. you can see that in the code below.
My problem is when I check the data on the frontend side am getting a blank array as a response.
I have double checked if I missed a variable or used a different API call, but no. I am exactly calling this custom service function but for some reason, I can't get the data.
Is it because of the usage of await/Promise on the 'result.roles', 'result.permission'? Did I use some bad practices?
async find (params) {
let result = {};
result.roles = [];
result.rolePermissions = [];
result.permissionsModules = [];
const permissionGroupModel = this.app.service('permission-groups').Model;
const rolesModel = this.app.service('roles').Model;
const permissionsModel = this.app.service('permissions').Model;
//all permission-groups are getting fine
result.permissionsModules = await new Promise(function(resolve, reject) {
permissionGroupModel.find().populate('permissions').exec((err,response)=>{
resolve(response);
})
});
//all roles are getting fine
result.roles = await new Promise(function(resolve, reject) {
rolesModel.find().then(response=>{
resolve(response);
})
});
//all permissions are getting fine
result.permissions = await new Promise(function(resolve, reject) {
permissionsModel.find().then(response=>{
resolve(response);
})
});
//here is iam having trouble..
result.rolePermissions = await new Promise(function(resolve, reject) {
let rolePerms = [];
result.roles.forEach(role => {
rolePerms[role.name] = {};
result.permissions.forEach(permission => {
rolePerms[role.name][permission.name] = (role.permissions.indexOf(permission._id) > -1)?true:false;
});
});
resolve(rolePerms);
});
//I have consoled result.rolePermissions and it shows the currect data.but when this data getting as null array in api call response
console.log('result.rolePermissions')
console.log(result.rolePermissions)
return {
status:true,
test:data.rolePermissions,
data:result
}
}