我有一个自定义服务,它从多个猫鼬模型中获取数据并使用这些数据创建一个数组并返回给 API。
一切正常。我可以控制新的对象数组“result.rolePermissions”。您可以在下面的代码中看到这一点。
我的问题是当我检查前端的数据时,我得到一个空白数组作为响应。
我已经仔细检查了我是否错过了一个变量或使用了不同的 API 调用,但没有。我正在调用此自定义服务功能,但由于某种原因,我无法获取数据。
是因为在“result.roles”、“result.permission”上使用了 await/Promise 吗?我使用了一些不好的做法吗?
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
}
}