0

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
    }
  }
Rameez Rami
  • 5,322
  • 2
  • 29
  • 36

2 Answers2

1

There are a couple things that can be done to improve this code. First, anything of the form...

x = await new Promise(function(resolve, reject) {
    promiseReturningFunction(params).then(response => {
        resolve(response);
    });
});

...can be rewritten as...

x = await promiseReturningFunction(params);

...because, when you have a promise-returning-function, there's no need to create another promise (with new Promise).

The code where you indicate trouble doesn't appear to need a promise at all...

//here is iam having trouble.. 
result.rolePermissions = [];
result.roles.forEach(role => {
  rolePerms[role.name] = {};
  result.permissions.forEach(permission => {
    rolePerms[role.name][permission.name]  (role.permissions.indexOf(permission._id) > -1)?true:false;
  });
});

Finally, I'm puzzled by the use of data in this code...

return {
  status:true,
  test:data.rolePermissions,
  data:result
}

Is it possible that, while your use of Promises was non-standard and confusing, that code was pretty much working, and the real problem comes from passing data.rolePermissions rather than the object you just built and tested with the log (result.rolePermissions)?

danh
  • 62,181
  • 10
  • 95
  • 136
  • oops "data.rolePermissions" its a typo in shared code. actual code is "result.rolePermissions" – Rameez Rami Oct 30 '18 at 14:42
  • btw this issue was fixed... i have no idea how it was fixed.. i just restarted feathers service. but many thanks for letting me know about the usage of ```x = await promiseReturningFunction(params);``` – Rameez Rami Oct 30 '18 at 14:44
0

The issue was fixed when i restarted the feathers service. I am not deleting the question because the other answer may share some knowledge

Rameez Rami
  • 5,322
  • 2
  • 29
  • 36