-
At work I work with Spring and this use case is rather trivial as accessing to But I struggle a bit to do it with Adonis.
I could obviously add a hook on the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey there! Unfortunately, there isn't a great way within JavaScript to make the automatic retrieval of an asynchronous property work as it can in Spring. Instead, you'll need to preload (eager) or load (lazy) the relationship, then you can access its properties. For example: export default class UserPolicy extends BasePolicy {
async before(user: User | null) {
if (user === null) return
await user.load('roles')
if (user.roles.some((role) => role.code === 'admin')) {
return true
}
}
// ...
} Edit: Adding |
Beta Was this translation helpful? Give feedback.
Hey there! Unfortunately, there isn't a great way within JavaScript to make the automatic retrieval of an asynchronous property work as it can in Spring.
Instead, you'll need to preload (eager) or load (lazy) the relationship, then you can access its properties.
https://lucid.adonisjs.com/docs/relationships#lazy-load-relationships
For example:
Edit: Adding
async
to examplebefore
method