-
Hello there, i have a problem. I want to check user role, i do this in my service class, where i'm throwing an error. Middleware have try catch construction where i'm use this service. and my service But my process will stop in the console, and the server will stop. What can I do to keep my server from stopping? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
And can i get role properties without model's load method? |
Beta Was this translation helpful? Give feedback.
-
If I had to guess I'd say it's because your As for your second question, in order to get the role properties you'll either need to load it or query it separately. Typically, what I do is have a seed that explicitly sets the role's ids. Then, define an enum within my application called For example: // contracts/Enums/Roles.ts
enum Roles {
ADMIN = 1,
USER = 2
}
export default Roles // app/Services/RoleService.ts
import Roles from 'Contracts/Enums/Roles'
export default class RoleService {
public static checkAdminRole(user: User) {
return user.roleId === Roles.ADMIN
}
} |
Beta Was this translation helpful? Give feedback.
If I had to guess I'd say it's because your
this.checkAdminRole
call in your middleware is missingawait
, since the method is async.As for your second question, in order to get the role properties you'll either need to load it or query it separately. Typically, what I do is have a seed that explicitly sets the role's ids. Then, define an enum within my application called
Roles
with each role record. This way I can perform the check without needing to make an additional database call.For example: