简体   繁体   中英

Feathers custom service response works in console but getting empty array on API response

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
    }
  }

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 )?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM